Tizen 2.4 SDK Rev6 Release
[framework/appfw/aul-1.git] / am_daemon / amd_launch.c
1 /*
2  *  aul
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@samsung.com>
7  *
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  *
20  */
21
22 #define _GNU_SOURCE
23 #include <signal.h>
24 #include <Ecore_X.h>
25 #include <Ecore_Input.h>
26 #ifdef _APPFW_FEATURE_AMD_KEY
27 #include <utilX.h>
28 #endif
29 #include <Ecore.h>
30 #include <Evas.h>
31 #include <Ecore_Evas.h>
32 #include <security-server.h>
33 #include <aul_svc.h>
34 #include <bundle.h>
35 #include <bundle_internal.h>
36 #include <aul.h>
37 #include <aul_svc.h>
38 #include <aul_svc_priv_key.h>
39 #include <glib.h>
40 #ifdef _APPFW_FEATURE_APP_CHECKER
41 #include <app-checker-server.h>
42 #endif
43 #include <string.h>
44 #include <stdlib.h>
45 #include <stdio.h>
46 #include <stdbool.h>
47 #include <sys/types.h>
48 #include <sys/stat.h>
49 #include <fcntl.h>
50 #include <sys/prctl.h>
51 #include <sys/resource.h>
52 #include <pkgmgr-info.h>
53 #include <vconf.h>
54 #include <proc_stat.h>
55 #include <poll.h>
56 #include <ttrace.h>
57 #include <sys/smack.h>
58 #include <security-server-perm.h>
59
60 #ifdef _APPFW_FEATURE_PRIVATE_SERVICE
61 #include <cert-service.h>
62 #endif
63
64 #include "amd_config.h"
65 #include "amd_launch.h"
66 #include "amd_appinfo.h"
67 #include "amd_status.h"
68 #include "amd_key.h"
69 #include "app_sock.h"
70 #include "simple_util.h"
71 #include "launch.h"
72 #include "app_signal.h"
73 #include "amd_app_group.h"
74 #include "amd_request.h"
75
76 #define PREEXEC_ACTIVATE
77 #include "preexec.h"
78
79 #define DAC_ACTIVATE
80 #include "access_control.h"
81
82 #ifdef _APPFW_FEATURE_EXPANSION_PKG_INSTALL
83 #include "appfw_env.h"
84 #endif
85
86 #define TERM_WAIT_SEC 3
87 #define INIT_PID 1
88
89 #define AUL_PR_NAME                     16
90 #define PATH_APP_ROOT "/opt/usr/apps"
91 #define PATH_DATA "/data"
92 #define SDK_CODE_COVERAGE "CODE_COVERAGE"
93 #define SDK_DYNAMIC_ANALYSIS "DYNAMIC_ANALYSIS"
94 #define PATH_DA_SO "/home/developer/sdk_tools/da/da_probe.so"
95
96 #ifdef _APPFW_FEATURE_FAKE_EFFECT
97 #define PHONE_ORIENTATION_MODE "memory/private/sensor/10001"
98 #define PHONE_ROTATE_LOCK "db/setting/auto_rotate_screen"
99 #endif
100
101 #define SYS_MIN_CPU_LOCK_FILE "/sys/devices/system/cpu/cpufreq/slp/min_cpu_lock"
102 #define MIN_CPU_LCK_CNT 0
103 #define MAX_CPU_LCK_CNT 2
104
105 #define HIDE_INDICATOR 0
106 #define SHOW_INDICATOR 1
107
108 #define PROC_STATUS_LAUNCH 0
109 #define PROC_STATUS_FG 3
110 #define PROC_STATUS_BG 4
111 #ifdef _APPFW_FEATURE_CPU_BOOST
112 #define APP_BOOSTING_PERIOD 1500 //msec
113 #endif
114
115 #ifdef _APPFW_FEATURE_PRIVATE_SERVICE
116 #define OSP_K_LAUNCH_TYPE   "__OSP_LAUNCH_TYPE__"
117 #define OSP_V_LAUNCH_TYPE_DATACONTROL   "datacontrol"
118 #endif
119
120 static char *amd_cmdline;
121
122 struct appinfomgr *_laf;
123
124 DBusConnection *conn;
125 #ifdef _APPFW_FEATURE_AMD_KEY
126 guint grab_timer_id;
127 #endif
128
129 static GList *_fgmgr_list;
130
131 struct fgmgr {
132         guint tid;
133         int pid;
134 };
135
136 #if 0
137 /*Unused data structure. Will be removed*/
138 typedef struct {
139         char *pkg_name;         /* package */
140         char *app_path;         /* exec */
141         char *original_app_path;        /* exec */
142         int multiple;           /* x_slp_multiple */
143         char *pkg_type;
144 } app_info_from_pkgmgr;
145 #endif
146
147 static GList *_kill_list;
148
149 struct ktimer {
150         pid_t pid;
151         char *group;
152         guint tid; /* timer ID */
153         struct cginfo *cg;
154 };
155
156 static const char *atom_name = "_E_COMP_FAKE_LAUNCH_IMAGE"; // Atomic ID string
157 static Ecore_X_Atom ATOM_IMAGE_EFFECT; //Atomic ID
158 static void __amd_effect_image_file_set(char *image_file);
159 static void __amd_send_message_to_e17(int screenmode, const char * indicator, int effect_type, int theme);
160 static void __set_reply_handler(int fd, int pid, int clifd, int cmd);
161 static void __real_send(int clifd, int ret);
162 static int __send_proc_prelaunch_signal(const char *appid, const char *pkgid, int attribute);
163 int invoke_dbus_method_sync(const char *dest, const char *path,
164                             const char *interface, const char *method,
165                             const char *sig, char *param[]);
166
167 static void _set_sdk_env(const char* appid, char* str) {
168         char buf[MAX_LOCAL_BUFSZ];
169         int ret;
170
171         _D("key : %s / value : %s", AUL_K_SDK, str);
172         /* http://gcc.gnu.org/onlinedocs/gcc/Cross_002dprofiling.html*/
173         /* GCOV_PREFIX contains the prefix to add to the absolute paths in the object file. */
174         /*              Prefix can be absolute, or relative. The default is no prefix.  */
175         /* GCOV_PREFIX_STRIP indicates the how many initial directory names */
176         /*              to stripoff the hardwired absolute paths. Default value is 0. */
177         if (strncmp(str, SDK_CODE_COVERAGE, strlen(str)) == 0) {
178                 snprintf(buf, MAX_LOCAL_BUFSZ, PATH_APP_ROOT"/%s"PATH_DATA, appid);
179                 ret = setenv("GCOV_PREFIX", buf, 1);
180                 _D("GCOV_PREFIX : %d", ret);
181                 ret = setenv("GCOV_PREFIX_STRIP", "4096", 1);
182                 _D("GCOV_PREFIX_STRIP : %d", ret);
183         } else if (strncmp(str, SDK_DYNAMIC_ANALYSIS, strlen(str)) == 0) {
184                 ret = setenv("LD_PRELOAD", PATH_DA_SO, 1);
185                 _D("LD_PRELOAD : %d", ret);
186         }
187 }
188
189 #define USE_ENGINE(engine) setenv("ELM_ENGINE", engine, 1);
190
191 static void _set_env(const char *appid, bundle * kb, const char *hwacc)
192 {
193         const char *str;
194         const char **str_array;
195         int len;
196         int i;
197
198         setenv("PKG_NAME", appid, 1);
199
200         USE_ENGINE("gl")
201
202         str = bundle_get_val(kb, AUL_K_STARTTIME);
203         if (str != NULL)
204                 setenv("APP_START_TIME", str, 1);
205
206         if(bundle_get_type(kb, AUL_K_SDK) & BUNDLE_TYPE_ARRAY) {
207                 str_array = bundle_get_str_array(kb, AUL_K_SDK, &len);
208                 if(str_array != NULL) {
209                         for (i = 0; i < len; i++) {
210                                 _D("index : [%d]", i);
211                                 _set_sdk_env(appid, (char *)str_array[i]);
212                         }
213                 }
214         } else {
215                 str = bundle_get_val(kb, AUL_K_SDK);
216                 if(str != NULL) {
217                         _set_sdk_env(appid, (char *)str);
218                 }
219         }
220         if (hwacc != NULL)
221                 setenv("HWACC", hwacc, 1);
222 }
223
224 static void __set_oom(void)
225 {
226         char buf[MAX_OOM_ADJ_BUFSZ] = {0,};
227         FILE *fp = NULL;
228
229         /* we should reset oomadj value as default because child
230         inherits from parent oom_adj*/
231         snprintf(buf, MAX_OOM_ADJ_BUFSZ, "/proc/%d/oom_score_adj", getpid());
232         fp = fopen(buf, "w");
233         if (fp == NULL)
234                 return;
235         fprintf(fp, "%d", 100);
236         fclose(fp);
237 }
238
239 static void _prepare_exec(const char *appid, bundle *kb)
240 {
241         const struct appinfo *ai;
242         const char *app_path = NULL;
243         const char *pkg_type = NULL;
244         char *file_name;
245         char process_name[AUL_PR_NAME];
246         const char *hwacc;
247         int ret;
248
249         setsid();
250
251         signal(SIGINT, SIG_DFL);
252         signal(SIGTERM, SIG_DFL);
253         signal(SIGCHLD, SIG_DFL);
254
255         __set_oom();
256
257         ai = appinfo_find(_laf, appid);
258
259         app_path = appinfo_get_value(ai, AIT_EXEC);
260         pkg_type = appinfo_get_value(ai, AIT_TYPE);
261         hwacc = appinfo_get_value(ai, AIT_HWACC);
262
263         __preexec_run(pkg_type, appid, app_path);
264
265         /* SET PRIVILEGES*/
266          SECURE_LOGD("appid : %s / pkg_type : %s / app_path : %s ", appid, pkg_type, app_path);
267         if (pkg_type && strncmp(pkg_type, "wgt", 3) !=0 && (ret = __set_access(appid, pkg_type, app_path)) < 0) {
268                  _D("fail to set privileges - check your package's credential : %d\n", ret);
269                 return;
270         }
271
272         /* SET DUMPABLE - for coredump*/
273         prctl(PR_SET_DUMPABLE, 1);
274
275         /* SET PROCESS NAME*/
276         if (app_path == NULL) {
277                 _D("app_path should not be NULL - check menu db");
278                 return;
279         }
280         file_name = strrchr(app_path, '/') + 1;
281         if (file_name == NULL) {
282                 _D("can't locate file name to execute");
283                 return;
284         }
285         memset(process_name, '\0', AUL_PR_NAME);
286         snprintf(process_name, AUL_PR_NAME, "%s", file_name);
287         prctl(PR_SET_NAME, process_name);
288
289         /* SET ENVIROMENT*/
290         _set_env(appid, kb, hwacc);
291 }
292
293 static char **__create_argc_argv(bundle * kb, int *margc)
294 {
295         char **argv;
296         int argc;
297
298         argc = bundle_export_to_argv(kb, &argv);
299
300         *margc = argc;
301         return argv;
302 }
303 static void _do_exec(const char *cmd, const char *group, bundle *kb)
304 {
305         gchar **argv;
306         gint argc;
307         char **b_argv;
308         int b_argc;
309         gboolean b;
310
311         b = g_shell_parse_argv(cmd, &argc, &argv, NULL);
312
313         if (kb) {
314                 b_argv = __create_argc_argv(kb, &b_argc);
315                 b_argv[0] = strdup(argv[0]);
316                 _prepare_exec(group, kb);
317                 execv(b_argv[0], b_argv);
318         }
319
320         if (b) {
321                 _prepare_exec(group, kb);
322                 execv(argv[0], argv);
323         }
324
325         _E("exec error: %s", strerror(errno));
326         g_strfreev(argv);
327 }
328
329 static inline int __send_app_launch_signal(int launch_pid, const char *app_id)
330 {
331         DBusMessage *message;
332
333         if (conn == NULL)
334                 return -1;
335
336         message = dbus_message_new_signal(AUL_DBUS_PATH,
337                                           AUL_DBUS_SIGNAL_INTERFACE,
338                                           AUL_DBUS_APPLAUNCH_SIGNAL);
339
340         if (dbus_message_append_args(message,
341                                 DBUS_TYPE_UINT32, &launch_pid,
342                                 DBUS_TYPE_STRING, &app_id,
343                                 DBUS_TYPE_INVALID) == FALSE) {
344                 _E("Failed to load data error");
345                 return -1;
346         }
347
348         if (dbus_connection_send(conn, message, NULL) == FALSE) {
349                 _E("dbus send error");
350                 return -1;
351         }
352
353         dbus_connection_flush(conn);
354         dbus_message_unref(message);
355
356         _W("send launch signal done: %d, %s", launch_pid, app_id);
357
358         return 0;
359 }
360
361 static int __send_watchdog_signal(int pid, int signal_num)
362 {
363         DBusMessage *message;
364
365         if (conn == NULL)
366                 return -1;
367
368         if (!_get_platform_ready()) {
369                 _E("[Info]_get_platform_ready return false");
370                 return -1;
371         }
372
373 #ifdef _APPFW_FEATURE_COOLDOWN_MODE_SUPPORT
374         if(_status_get_cooldown_status() == COOLDOWN_LIMIT) {
375                 _E("[Info]cooldown status : LimitAction");
376                 return -1;
377         }
378 #endif // _APPFW_FEATURE_COOLDOWN_MODE_SUPPORT
379
380         message = dbus_message_new_signal(RESOURCED_PROC_OBJECT,
381                                           RESOURCED_PROC_INTERFACE,
382                                           RESOURCED_PROC_WATCHDOG_SIGNAL);
383
384         if (dbus_message_append_args(message,
385                                      DBUS_TYPE_INT32, &pid,
386                                      DBUS_TYPE_INT32, &signal_num,
387                                      DBUS_TYPE_INVALID) == FALSE) {
388                 _E("Failed to load data error");
389                 return -1;
390         }
391
392         if (dbus_connection_send(conn, message, NULL) == FALSE) {
393                 _E("dbus send error");
394                 return -1;
395         }
396
397         dbus_connection_flush(conn);
398         dbus_message_unref(message);
399
400         _W("send a watchdog signal done: %d", pid);
401
402         return 0;
403 }
404
405 static int __send_proc_prelaunch_signal(const char *appid, const char *pkgid, int attribute)
406 {
407         DBusMessage *message;
408
409         if (conn == NULL)
410                 return -1;
411
412         message = dbus_message_new_signal(RESOURCED_PROC_OBJECT,
413                                           RESOURCED_PROC_INTERFACE,
414                                           RESOURCED_PROC_PRELAUNCH_SIGNAL);
415
416         if (dbus_message_append_args(message,
417                                          DBUS_TYPE_STRING, &appid,
418                                          DBUS_TYPE_STRING, &pkgid,
419                                      DBUS_TYPE_INT32, &attribute,
420                                      DBUS_TYPE_INVALID) == FALSE) {
421                 _E("Failed to load data error");
422                 return -1;
423         }
424
425         if (dbus_connection_send(conn, message, NULL) == FALSE) {
426                 _E("dbus send error");
427                 return -1;
428         }
429
430         dbus_connection_flush(conn);
431         dbus_message_unref(message);
432
433         SECURE_LOGW("send a prelaunch signal done: appid(%s) pkgid(%s) attribute(%x)", appid, pkgid, attribute);
434
435         return 0;
436 }
437
438 static int __check_cmdline(int ret)
439 {
440         char *cmdline;
441         int wait_count;
442         int cmdline_changed = 0;
443         int cmdline_exist = 0;
444
445         if (ret <= 1)
446                 return -1;
447
448         /* check normally was launched?*/
449         wait_count = 1;
450         do {
451                 cmdline = __proc_get_cmdline_bypid(ret);
452                 if (cmdline == NULL) {
453                         _E("error founded when being launched with %d", ret);
454                         if (cmdline_exist || cmdline_changed) {
455                                 _E("The app process might be terminated while we are wating %d", ret);
456                                 break;
457                         }
458                 } else if (strcmp(cmdline, amd_cmdline)) {
459                         free(cmdline);
460                         cmdline_changed = 1;
461                         break;
462                 } else {
463                         cmdline_exist = 1;
464                         free(cmdline);
465                 }
466
467                 _D("-- now wait to change cmdline --");
468                 usleep(50 * 1000);      /* 50ms sleep*/
469                 wait_count++;
470         } while (wait_count <= 20);     /* max 50*20ms will be sleep*/
471
472         if ((!cmdline_exist) && (!cmdline_changed)) {
473                 _E("cmdline_exist 0 & cmdline_changed 0");
474                 return -1;
475         }
476
477         if (!cmdline_changed)
478                 _E("process launched, but cmdline not changed");
479
480         return ret;
481 }
482
483 int start_process(const char *appid, const char *cmd, bundle *kb)
484 {
485         int r;
486         pid_t p;
487
488         p = fork();
489         switch (p) {
490         case 0: /* child process */
491                 _D("start application");
492                 _signal_unblock_sigchld();
493 #ifdef _APPFW_FEATURE_PRIORITY_CHANGE
494                 r = setpriority(PRIO_PROCESS, 0, 0);
495                 if (r == -1)
496                 {
497                         SECURE_LOGE("Setting process (%d) priority to 0 failed, errno: %d (%s)",
498                                         getpid(), errno, strerror(errno));
499                 }
500 #endif
501                 _do_exec(cmd, appid, kb);
502                 /* exec error */
503
504                 exit(0);
505                 break;
506         case -1:
507                 _E("application start: fork: %s", strerror(errno));
508                 r = -1;
509                 break;
510         default: /* parent process */
511                 _W("child process: %d", p);
512                 r = __check_cmdline(p);
513                 if(r > 0)
514                         __send_app_launch_signal(r, appid);
515                 else
516                         _E("cmdline change failed.");
517                 break;
518         }
519
520         return r;
521 }
522
523 static int __check_ver(const char *required, const char *actual)
524 {
525         int ret = 0;
526         if (required && actual) {
527                 ret = strverscmp(required, actual); // should 0 or less
528                 if (ret < 1)
529                         return 1;
530         }
531
532         return 0;
533 }
534
535 static int __get_prelaunch_attribute(const struct appinfo *ai)
536 {
537         int attribute_val = 0;
538         const char *attribute_str = NULL;
539
540         attribute_str = appinfo_get_value(ai, AIT_ALLOWED_BG);
541         if (attribute_str && strncmp(attribute_str, "ALLOWED_BG", sizeof("ALLOWED_BG")) == 0) {
542                 attribute_val |= RESOURCED_ALLOWED_BG_ATTRIBUTE;
543         }
544
545 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
546         attribute_val |= RESOURCED_BACKGROUND_MANAGEMENT_ATTRIBUTE;
547 #endif
548
549         attribute_str = appinfo_get_value(ai, AIT_API_VER);
550         if (attribute_str && __check_ver("2.4", attribute_str)) {
551                 attribute_val |= RESOURCED_API_VER_2_4_ATTRIBUTE;
552         }
553
554         return attribute_val;
555 }
556
557 int _start_srv(const struct appinfo *ai)
558 {
559         int r;
560         bundle *b = NULL;
561         const char *group;
562         const char *cmd;
563         const char *pkgid;
564         const char *appid = NULL;
565         int prelaunch_attribute = 0;
566
567         group = appinfo_get_filename(ai);
568
569         cmd = appinfo_get_value(ai, AIT_EXEC);
570         if (!cmd) {
571                 _E("start service: '%s' has no exec", group);
572                 return -1;
573         }
574
575         appid = appinfo_get_value(ai, AIT_NAME);
576         pkgid = appinfo_get_value(ai, AIT_PKGID);
577
578         prelaunch_attribute = __get_prelaunch_attribute(ai);
579         if ((prelaunch_attribute & RESOURCED_ALLOWED_BG_ATTRIBUTE) ||
580                 !(prelaunch_attribute & RESOURCED_API_VER_2_4_ATTRIBUTE)) {
581                 SECURE_LOGD("[__SUSPEND__] allowed background, appid :%s, app_type: %s, api version: %s",
582                         appid, APP_TYPE_SERVICE, appinfo_get_value(ai,  AIT_API_VER));
583                 if (b == NULL) {
584                         b = bundle_create();
585                 }
586
587                 bundle_add(b, AUL_K_ALLOWED_BG, "ALLOWED_BG");
588         }
589
590         __send_proc_prelaunch_signal(appid, pkgid, prelaunch_attribute);
591
592         r = start_process(group, cmd, b);
593         if (b) {
594                 bundle_free(b);
595                 b = NULL;
596         }
597
598         if (r == -1) {
599                 _E("start service: '%s': failed", group);
600
601                 return -1;
602         }
603
604         aul_send_app_launch_request_signal(r, group, pkgid, APP_TYPE_SERVICE);
605         _status_add_app_info_list(group, cmd, NULL, r, -1, 0);
606
607         return 0;
608 }
609
610 static void _free_kt(struct ktimer *kt)
611 {
612         if (!kt)
613                 return;
614
615         free(kt->group);
616         free(kt);
617 }
618
619 static void _kill_pid(struct cginfo *cg, const char *group, pid_t pid)
620 {
621         int r;
622
623         if (pid <= INIT_PID) /* block sending to all process or init */
624                 return;
625
626         /* TODO: check pid exist in group */
627
628         r = kill(pid, 0);
629         if (r == -1) {
630                 _E("send SIGKILL: pid %d not exist", pid);
631                 return;
632         }
633
634         r = kill(pid, SIGKILL);
635         if (r == -1)
636                 _E("send SIGKILL: %s", strerror(errno));
637 }
638
639 static gboolean _ktimer_cb(gpointer data)
640 {
641         struct ktimer *kt = data;
642
643         _kill_pid(kt->cg, kt->group, kt->pid);
644         _kill_list = g_list_remove(_kill_list, kt);
645         _free_kt(kt);
646
647         return FALSE;
648 }
649
650 static void _add_list(struct cginfo *cg, const char *group, pid_t pid)
651 {
652         struct ktimer *kt;
653
654         kt = calloc(1, sizeof(*kt));
655         if (!kt)
656                 return;
657
658         kt->pid = pid;
659         kt->group = strdup(group);
660         if (!kt->group) {
661                 free(kt);
662                 return;
663         }
664
665         kt->tid = g_timeout_add_seconds(TERM_WAIT_SEC, _ktimer_cb, kt);
666
667         _kill_list = g_list_append(_kill_list, kt);
668 }
669
670 static inline void _del_list(GList *l)
671 {
672         struct ktimer *kt;
673
674         if (!l)
675                 return;
676
677         kt = l->data;
678
679         g_source_remove(kt->tid);
680         _free_kt(kt);
681         _kill_list = g_list_delete_link(_kill_list, l);
682 }
683
684 static int _kill_pid_cb(void *user_data, const char *group, pid_t pid)
685 {
686         int r;
687
688         if (pid <= INIT_PID) /* block sending to all process or init */
689                 return 0;
690
691         r = kill(pid, SIGTERM);
692         if (r == -1)
693                 _E("send SIGTERM: %s", strerror(errno));
694
695         _add_list(user_data, group, pid);
696
697         return 0;
698 }
699
700 void service_release(const char *group)
701 {
702         GList *l;
703         GList *d;
704
705         if (!group || !*group)
706                 return;
707
708         group = FILENAME(group);
709
710         d = NULL;
711         for (l = _kill_list; l; l = g_list_next(l)) {
712                 struct ktimer *k = l->data;
713
714                 _del_list(d);
715
716                 if (k->group && !strcmp(k->group, group))
717                         d = l;
718                 else
719                         d = NULL;
720         }
721
722         _del_list(d);
723 }
724
725 int _send_to_sigkill(int pid)
726 {
727         int pgid;
728
729         pgid = getpgid(pid);
730         if (pgid <= 1)
731                 return -1;
732
733         if (killpg(pgid, SIGKILL) < 0)
734                 return -1;
735
736         return 0;
737 }
738 int _resume_app(int pid, int clifd)
739 {
740         int dummy;
741         int ret;
742         if ((ret =
743              __app_send_raw_with_delay_reply(pid, APP_RESUME_BY_PID, (unsigned char *)&dummy,
744                             sizeof(int))) < 0) {
745                 if (ret == -EAGAIN)
746                         _E("resume packet timeout error");
747                 else {
748                         _E("raise failed - %d resume fail\n", pid);
749                         _E("we will term the app - %d\n", pid);
750                         _send_to_sigkill(pid);
751                         ret = -1;
752                 }
753                 __real_send(clifd, ret);
754         }
755         _W("resume done\n");
756
757         if (ret > 0)
758                 __set_reply_handler(ret, pid, clifd, APP_RESUME_BY_PID);
759
760         return ret;
761 }
762
763 int _pause_app(int pid, int clifd)
764 {
765         int dummy;
766         int ret;
767         if ((ret =
768             __app_send_raw_with_delay_reply(pid, APP_PAUSE_BY_PID, (unsigned char *)&dummy,
769                             sizeof(int))) < 0) {
770                 if (ret == -EAGAIN)
771                         _E("pause packet timeout error");
772                 else {
773                         _E("iconify failed - %d pause fail\n", pid);
774                         _E("we will term the app - %d\n", pid);
775                         _send_to_sigkill(pid);
776                         ret = -1;
777                 }
778         }
779
780         _D("pause done\n");
781         close(clifd);
782
783         return ret;
784 }
785
786 int _fake_launch_app(int cmd, int pid, bundle * kb, int clifd)
787 {
788         int datalen;
789         int ret;
790         bundle_raw *kb_data = NULL;
791
792         if (!kb){
793                 __real_send(clifd, -EINVAL);
794                 return -EINVAL;
795         }
796
797         ret = bundle_encode(kb, &kb_data, &datalen);
798         if (ret != BUNDLE_ERROR_NONE) {
799                 __real_send(clifd, -EINVAL);
800                 return -EINVAL;
801         }
802         if ((ret = __app_send_raw_with_delay_reply(pid, cmd, kb_data, datalen)) < 0) {
803                 _E("error request fake launch - error code = %d", ret);
804                 __real_send(clifd, ret);
805         }
806         free(kb_data);
807
808         if (ret > 0)
809                 __set_reply_handler(ret, pid, clifd, cmd);
810
811         return ret;
812 }
813
814 static void __real_send(int clifd, int ret)
815 {
816         if(clifd <= 0) {
817                 return;
818         }
819         if (send(clifd, &ret, sizeof(int), MSG_NOSIGNAL) < 0) {
820                 if (errno == EPIPE) {
821                         _E("send failed due to EPIPE.\n");
822                 }
823                 _E("send fail to client");
824         }
825
826         close(clifd);
827 }
828
829 static gboolean __au_glib_check(GSource *src)
830 {
831         GSList *fd_list;
832         GPollFD *tmp;
833
834         fd_list = src->poll_fds;
835         do {
836                 tmp = (GPollFD *) fd_list->data;
837                 if ((tmp->revents & (POLLIN | POLLPRI)))
838                         return TRUE;
839                 fd_list = fd_list->next;
840         } while (fd_list);
841
842         return FALSE;
843 }
844
845 static gboolean __au_glib_dispatch(GSource *src, GSourceFunc callback,
846                 gpointer data)
847 {
848         callback(data);
849         return TRUE;
850 }
851
852 static gboolean __au_glib_prepare(GSource *src, gint *timeout)
853 {
854         return FALSE;
855 }
856
857 static GSourceFuncs funcs = {
858         .prepare = __au_glib_prepare,
859         .check = __au_glib_check,
860         .dispatch = __au_glib_dispatch,
861         .finalize = NULL
862 };
863
864 struct reply_info {
865         GSource *src;
866         GPollFD *gpollfd;
867         guint timer_id;
868         int clifd;
869         int pid;
870         int cmd;
871 };
872
873 static gboolean __reply_handler(gpointer data)
874 {
875         struct reply_info *r_info = (struct reply_info *) data;;
876         int fd = r_info->gpollfd->fd;
877         int len;
878         int res = 0;
879         int clifd = r_info->clifd;
880         int pid = r_info->pid;
881         int cmd = r_info->cmd;
882
883         len = recv(fd, &res, sizeof(int), 0);
884         if (len == -1) {
885                 if (errno == EAGAIN) {
886                         _E("recv timeout : %s", strerror(errno));
887                         res = -EAGAIN;
888                 } else {
889                         _E("recv error : %s", strerror(errno));
890                         res = -ECOMM;
891                 }
892         }
893         close(fd);
894
895         if(res < 0) {
896                 if (cmd == APP_TERM_BY_PID || cmd == APP_TERM_BGAPP_BY_PID) {
897                         __real_send(clifd, -1);
898                 } else if (cmd == APP_START_ASYNC || cmd == APP_PAUSE_BY_PID) {
899                         close(clifd);
900                 } else {
901                         __real_send(clifd, res);
902                 }
903         } else {
904                 if (cmd == APP_TERM_BY_PID || cmd == APP_TERM_BGAPP_BY_PID) {
905                         __real_send(clifd, 0);
906                 } else if (cmd == APP_START_ASYNC || cmd == APP_PAUSE_BY_PID) {
907                         close(clifd);
908                 } else {
909                         __real_send(clifd, pid);
910                 }
911         }
912
913         _W("listen fd(%d) , send fd(%d), pid(%d), cmd(%d)", fd, clifd, pid, cmd);
914
915         g_source_remove(r_info->timer_id);
916         g_source_remove_poll(r_info->src, r_info->gpollfd);
917         g_source_destroy(r_info->src);
918         g_free(r_info->gpollfd);
919         free(r_info);
920
921         return TRUE;
922 }
923
924 static gboolean __recv_timeout_handler(gpointer data)
925 {
926         struct reply_info *r_info = (struct reply_info *) data;
927         int fd = r_info->gpollfd->fd;
928         char *appid = NULL;
929         const struct appinfo *ai;
930         int task_manage = 0;
931
932         _E("application is not responding : pid(%d) cmd(%d)", r_info->pid, r_info->cmd);
933
934         close(fd);
935
936         switch (r_info->cmd) {
937         case APP_OPEN:
938         case APP_RESUME:
939         case APP_START:
940         case APP_START_RES:
941         case APP_START_ASYNC:
942 #ifdef _APPFW_FEATURE_MULTI_INSTANCE
943         case APP_START_MULTI_INSTANCE:
944 #endif
945                 appid = _status_app_get_appid_bypid(r_info->pid);
946                 if(appid == NULL)
947                         break;
948                 ai = appinfo_find(_laf, appid);
949                 if(ai == NULL) {
950                         _E("ai is NULL");
951                         break;
952                 }
953                 task_manage = appinfo_get_boolean(ai, AIT_TASKMANAGE);
954                 if(task_manage) {
955                         __send_watchdog_signal(r_info->pid, SIGKILL);
956                 }
957                 break;
958         case APP_TERM_BY_PID:
959         case APP_TERM_BGAPP_BY_PID:
960                 if (_send_to_sigkill(r_info->pid) < 0) {
961                         _E("fail to killing - %d\n", r_info->pid);
962                         __real_send(r_info->clifd, -1);
963                 } else {
964                         __real_send(r_info->clifd, 0);
965                 }
966                 break;
967         }
968
969         g_source_remove_poll(r_info->src, r_info->gpollfd);
970         g_source_destroy(r_info->src);
971         g_free(r_info->gpollfd);
972         free(r_info);
973
974         return FALSE;
975 }
976
977 static void __set_reply_handler(int fd, int pid, int clifd, int cmd)
978 {
979         GPollFD *gpollfd;
980         GSource *src;
981         struct reply_info *r_info;
982
983         src = g_source_new(&funcs, sizeof(GSource));
984
985         gpollfd = (GPollFD *) g_malloc(sizeof(GPollFD));
986         if (gpollfd == NULL) {
987                 _E("out of memory");
988                 g_source_unref(src);
989                 return;
990         }
991         gpollfd->events = POLLIN;
992         gpollfd->fd = fd;
993
994         r_info = malloc(sizeof(*r_info));
995         if (r_info == NULL) {
996                 _E("out of memory");
997                 g_free(gpollfd);
998                 g_source_unref(src);
999                 return;
1000         }
1001
1002         r_info->clifd = clifd;
1003         r_info->pid = pid;
1004         r_info->src = src;
1005         r_info->gpollfd = gpollfd;
1006         r_info->cmd = cmd;
1007
1008         r_info->timer_id = g_timeout_add(5000, __recv_timeout_handler, (gpointer) r_info);
1009         g_source_add_poll(src, gpollfd);
1010         g_source_set_callback(src, (GSourceFunc) __reply_handler,
1011                         (gpointer) r_info, NULL);
1012         g_source_set_priority(src, G_PRIORITY_DEFAULT);
1013         g_source_attach(src, NULL);
1014
1015         _D("listen fd : %d, send fd : %d", fd, clifd);
1016 }
1017
1018 void _term_sub_app(int pid)
1019 {
1020         int dummy;
1021         int ret;
1022         char *appid = NULL;
1023         const char *pkgid = NULL;
1024         const char *type = NULL;
1025         const struct appinfo *ai = NULL;
1026
1027         appid = _status_app_get_appid_bypid(pid);
1028         ai = appinfo_find(_laf, appid);
1029         pkgid = appinfo_get_value(ai, AIT_PKGID);
1030         type = appinfo_get_value(ai, AIT_COMPTYPE);
1031
1032         aul_send_app_terminate_request_signal(pid, appid, pkgid, type);
1033         if ( (ret = __app_send_raw_with_noreply(pid, APP_TERM_BY_PID_ASYNC,
1034                                         (unsigned char *)&dummy, sizeof(int))) < 0) {
1035                 _E("terminate packet send error - use SIGKILL");
1036                 if (_send_to_sigkill(pid) < 0) {
1037                         _E("fail to killing - %d\n", pid);
1038                         return;
1039                 }
1040         }
1041 }
1042
1043 int _term_app(int pid, int clifd)
1044 {
1045         int dummy;
1046         int ret;
1047
1048         if (app_group_is_leader_pid(pid)) {
1049                 int cnt;
1050                 int *pids = NULL;
1051                 int i;
1052
1053                 app_group_get_group_pids(pid, &cnt, &pids);
1054                 if (cnt > 0) {
1055                         for (i = cnt-1 ; i>=0; i--) {
1056                                 if (i != 0)
1057                                         _term_sub_app(pids[i]);
1058                                 app_group_remove(pids[i]);
1059
1060                         }
1061                 }
1062                 if (pids)
1063                         free(pids);
1064         }
1065
1066         if ( (ret = __app_send_raw_with_delay_reply
1067             (pid, APP_TERM_BY_PID, (unsigned char *)&dummy, sizeof(int))) < 0) {
1068                 _D("terminate packet send error - use SIGKILL");
1069                 if (_send_to_sigkill(pid) < 0) {
1070                         _E("fail to killing - %d\n", pid);
1071                         __real_send(clifd, -1);
1072                         return -1;
1073                 }
1074                 __real_send(clifd, 0);
1075         }
1076         _D("term done\n");
1077         if (ret > 0)
1078                 __set_reply_handler(ret, pid, clifd, APP_TERM_BY_PID);
1079
1080         return 0;
1081 }
1082
1083 int _term_req_app(int pid, int clifd)
1084 {
1085         int dummy;
1086         int ret;
1087
1088         if ( (ret = __app_send_raw_with_delay_reply
1089                 (pid, APP_TERM_REQ_BY_PID, (unsigned char *)&dummy, sizeof(int))) < 0) {
1090                 _D("terminate req send error");
1091                 __real_send(clifd, ret);
1092         }
1093
1094         if (ret > 0)
1095                 __set_reply_handler(ret, pid, clifd, APP_TERM_REQ_BY_PID);
1096
1097         return 0;
1098 }
1099
1100 int _term_bgapp(int pid, int clifd)
1101 {
1102         int dummy;
1103         int fd;
1104
1105         if (app_group_is_leader_pid(pid)) {
1106                 int cnt;
1107                 int *pids = NULL;
1108                 int i;
1109                 int status = -1;
1110
1111                 app_group_get_group_pids(pid, &cnt, &pids);
1112                 if (cnt > 0) {
1113                         status = _status_get_app_info_status(pids[cnt-1]);
1114                         if(status == STATUS_BG) {
1115                                 for (i = cnt-1 ; i>=0; i--) {
1116                                         if (i != 0)
1117                                                 _term_sub_app(pids[i]);
1118                                         app_group_remove(pids[i]);
1119                                 }
1120                         }
1121                 }
1122                 if (pids)
1123                         free(pids);
1124         }
1125
1126         if ( (fd = __app_send_raw_with_delay_reply
1127             (pid, APP_TERM_BGAPP_BY_PID, (unsigned char *)&dummy, sizeof(int))) < 0) {
1128                 _D("terminate packet send error - use SIGKILL");
1129                 if (_send_to_sigkill(pid) < 0) {
1130                         _E("fail to killing - %d\n", pid);
1131                         __real_send(clifd, -1);
1132                         return -1;
1133                 }
1134                 __real_send(clifd, 0);
1135         }
1136         _D("term_bgapp done\n");
1137         if (fd > 0)
1138                 __set_reply_handler(fd, pid, clifd, APP_TERM_BGAPP_BY_PID);
1139
1140         return 0;
1141 }
1142
1143 #include <dirent.h>
1144 #include <sqlite3.h>
1145 static int __launchpad_update_task_managed_field(const char* app_id, int task_managed)
1146 {
1147         sqlite3 *db = NULL;
1148         char *sqlite3_error_msg = NULL;
1149
1150         if (sqlite3_open("/opt/dbspace/.pkgmgr_parser.db", &db) != SQLITE_OK) {
1151             _E("sqlite3_open() failed! -> %s\n", sqlite3_errmsg(db));
1152             return -1;
1153         }
1154
1155         if (sqlite3_exec(db, "PRAGMA journal_mode = PERSIST", NULL, NULL, &sqlite3_error_msg) != SQLITE_OK) {
1156             _E("sqlite3_exec(\"PRAGMA journal_mode = PERSIST\") failed! -> %s", sqlite3_error_msg);
1157             sqlite3_free(sqlite3_error_msg);
1158             sqlite3_close(db);
1159             return -1;
1160         }
1161
1162         if (sqlite3_exec(db, "BEGIN EXCLUSIVE", NULL, NULL, &sqlite3_error_msg) != SQLITE_OK) {
1163                 _E("sqlite3_exec(\"BEGIN EXCLUSIVE\") failed! -> %s", sqlite3_error_msg);
1164             sqlite3_free(sqlite3_error_msg);
1165                 sqlite3_close(db);
1166                 return -1;
1167         }
1168
1169         char query[1024] = {0, };
1170         snprintf(query, 1024,"update package_app_info set app_taskmanage='%s' where app_id='%s'",
1171                 task_managed ? "true" : "false", app_id);
1172
1173         if (sqlite3_exec(db, query, NULL, NULL, &sqlite3_error_msg) != SQLITE_OK) {
1174                 _E("sqlite3_exec(\"%s\") failed! -> %s", query, sqlite3_error_msg);
1175                 sqlite3_free(sqlite3_error_msg);
1176                 return -1;
1177         }
1178
1179         if (sqlite3_exec(db, "COMMIT", NULL, NULL, NULL) != SQLITE_OK) {
1180                 _E("sqlite3_exec(\"COMMIT\") failed!");
1181                 if (sqlite3_exec(db, "ROLLBACK", NULL, NULL, NULL) != SQLITE_OK) {
1182                         _E("sqlite3_exec(\"ROLLBACK\") failed!");
1183                 }
1184                 sqlite3_close(db);
1185                 return -1;
1186         }
1187
1188         sqlite3_close(db);
1189
1190 return 0;
1191 }
1192
1193 static void __pre_launching_processing(const char* app_id)
1194 {
1195     const char* const PRE_LAUNCHING_LIST_DIR = "/opt/usr/etc/wrt_launchpad_daemon/pre_launching_list";
1196
1197     struct stat file_status;
1198     if (stat(PRE_LAUNCHING_LIST_DIR, &file_status) == 0) {
1199         if (S_ISDIR(file_status.st_mode)) {
1200             int ret;
1201             DIR *dir = NULL;
1202             struct dirent entry, *result = NULL;
1203
1204             dir = opendir(PRE_LAUNCHING_LIST_DIR);
1205
1206             if (dir) {
1207                 for (ret = readdir_r(dir, &entry, &result);
1208                      result != NULL && ret == 0;
1209                      ret = readdir_r(dir, &entry, &result)) {
1210                     if (strncmp(entry.d_name, ".", 2) == 0 ||
1211                         strncmp(entry.d_name, "..", 3) == 0) {
1212                         continue;
1213                     }
1214
1215                     if (strcmp(entry.d_name, app_id) == 0)
1216                     {
1217                         __launchpad_update_task_managed_field(app_id, 1);
1218                     }
1219                 }
1220
1221                 closedir(dir);
1222             }
1223             else {
1224                 _E("opendir(\"%s\") failed!", PRE_LAUNCHING_LIST_DIR);
1225             }
1226         }
1227     }
1228 }
1229
1230 static int __nofork_processing(int cmd, int pid, bundle * kb, int clifd)
1231 {
1232         int ret = -1;
1233 #ifdef _APPFW_FEATURE_CPU_BOOST
1234         const char *operation;
1235
1236         operation = bundle_get_val(kb, "__APP_SVC_OP_TYPE__");
1237
1238         //TODO: CPU boosting for relaunching will be removed.
1239         //Home screen requests CPU boosting on launching or relaunching during 200 msec.
1240         if (cmd == APP_OPEN ||
1241                         (operation != NULL && strncmp(operation, "http://tizen.org/appcontrol/operation/main", 512) == 0)) {
1242                 char *arr[2];
1243                 char val[32];
1244                 snprintf(val, sizeof(val), "%d", APP_BOOSTING_PERIOD);
1245                 arr[0] = val;
1246                 arr[1] = NULL;
1247                 ret = invoke_dbus_method_sync(SYSTEM_BUS_NAME, SYSTEM_OBJECT_PATH,
1248                                 SYSTEM_INTERFACE_NAME, SYSTEM_METHOD_NAME, "i", arr);
1249                 _D("%s-%s : %d", SYSTEM_INTERFACE_NAME, SYSTEM_METHOD_NAME, ret);
1250         }
1251 #endif
1252         _W("__nofork_processing, cmd: %d, pid: %d", cmd, pid);
1253         switch (cmd) {
1254         case APP_OPEN:
1255         case APP_RESUME:
1256                 _D("resume app's pid : %d\n", pid);
1257                 if ((ret = _resume_app(pid, clifd)) < 0)
1258                         _E("__resume_app failed. error code = %d", ret);
1259                 _D("resume app done");
1260                 break;
1261
1262         case APP_START:
1263         case APP_START_RES:
1264         case APP_START_ASYNC:
1265 #ifdef _APPFW_FEATURE_MULTI_INSTANCE
1266         case APP_START_MULTI_INSTANCE:
1267 #endif
1268                 _D("fake launch pid : %d\n", pid);
1269                 if ((ret = _fake_launch_app(cmd, pid, kb, clifd)) < 0)
1270                         _E("fake_launch failed. error code = %d", ret);
1271                 _D("fake launch done");
1272                 break;
1273
1274         default:
1275                 break;
1276         }
1277
1278         return ret;
1279 }
1280
1281 #if __CPU_FREQ_CON
1282 #include <sysman.h>
1283 static Eina_Bool
1284 __amd_sysman_restore_hz_timer_cb(void *data)
1285 {
1286    struct context *ctxt = data;
1287
1288    if (ctxt->timer)
1289      ctxt->timer = NULL;
1290
1291    sysman_release_cpu_min_frequency ();
1292
1293    _D("*******[1.6MHZ Support] Released\n " );
1294
1295    return ECORE_CALLBACK_CANCEL; // same as EINA_FALSE
1296 }
1297 #endif
1298
1299 struct context{
1300         Ecore_Timer *timer;
1301 };
1302
1303 #ifdef _APPFW_FEATURE_FAKE_EFFECT
1304 static void __amd_effect_image_file_set(char *image_file)
1305 {
1306         Ecore_X_Window root_win;
1307
1308         if(!_window_is_initialized())
1309                 return;
1310
1311         root_win = ecore_x_window_root_first_get();
1312         SECURE_LOGD("path : %s", image_file);
1313         ecore_x_window_prop_string_set(root_win, ATOM_IMAGE_EFFECT, image_file);
1314 }
1315
1316
1317 static void __amd_send_message_to_e17(int screenmode, const char * indicator, int effect_type, int theme)
1318 {
1319         traceBegin(TTRACE_TAG_APPLICATION_MANAGER, "AUL:AMD:EFFECT_START");
1320         Ecore_X_Window root_win;
1321         int ret;
1322
1323         if (!_window_is_initialized()) {
1324                 traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
1325                 return;
1326         }
1327
1328         root_win = ecore_x_window_root_first_get();
1329          _D("root win : %x",root_win);
1330         int screen_orientation[5]={0,0,270,180,90};
1331         if (screenmode > 4 || screenmode < 0)
1332                 screenmode=0;
1333
1334         if (strncmp(indicator, "true", 4) == 0) {
1335                 _D("[LAUNCHING EFFECT]: screen mode(%d), effect type(%d), theme(%d), indicator show",
1336                         screen_orientation[screenmode], effect_type, theme);
1337                 ret = ecore_x_client_message32_send (root_win, ATOM_IMAGE_EFFECT,
1338                         ECORE_X_EVENT_MASK_WINDOW_PROPERTY, effect_type,
1339                         screen_orientation[screenmode],
1340                         SHOW_INDICATOR, theme, 0);
1341
1342         } else {
1343                 _D("[LAUNCHING EFFECT]: screen mode(%d), effect type(%d), theme(%d), indicator show",
1344                         screen_orientation[screenmode], effect_type, theme);
1345                 ret = ecore_x_client_message32_send (root_win, ATOM_IMAGE_EFFECT,
1346                         ECORE_X_EVENT_MASK_WINDOW_PROPERTY, effect_type,
1347                         screen_orientation[screenmode],
1348                         HIDE_INDICATOR, theme, 0);
1349         }
1350         ecore_x_flush();
1351         _D("ecore_x_client_message32_send : %d",ret);
1352         traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
1353 }
1354 #endif
1355
1356 static int append_variant(DBusMessageIter *iter, const char *sig, char *param[])
1357 {
1358         char *ch;
1359         int i;
1360         int int_type;
1361         uint64_t int64_type;
1362
1363         if (!sig || !param)
1364                 return 0;
1365
1366         for (ch = (char*)sig, i = 0; *ch != '\0'; ++i, ++ch) {
1367                 switch (*ch) {
1368                 case 'i':
1369                         int_type = atoi(param[i]);
1370                         dbus_message_iter_append_basic(iter, DBUS_TYPE_INT32, &int_type);
1371                         break;
1372                 case 'u':
1373                         int_type = atoi(param[i]);
1374                         dbus_message_iter_append_basic(iter, DBUS_TYPE_UINT32, &int_type);
1375                         break;
1376                 case 't':
1377                         int64_type = atoi(param[i]);
1378                         dbus_message_iter_append_basic(iter, DBUS_TYPE_UINT64, &int64_type);
1379                         break;
1380                 case 's':
1381                         dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, param[i]);
1382                         break;
1383                 default:
1384                         return -EINVAL;
1385                 }
1386         }
1387
1388         return 0;
1389 }
1390
1391 int invoke_dbus_method_sync(const char *dest, const char *path,
1392                 const char *interface, const char *method,
1393                 const char *sig, char *param[])
1394 {
1395         DBusMessage *msg;
1396         DBusMessageIter iter;
1397         DBusMessage *reply;
1398         DBusError err;
1399         int r, ret;
1400
1401         msg = dbus_message_new_method_call(dest, path, interface, method);
1402         if (!msg) {
1403                 _E("dbus_message_new_method_call(%s:%s-%s)", path, interface, method);
1404                 return -EBADMSG;
1405         }
1406
1407         if( param ) {
1408                 dbus_message_iter_init_append(msg, &iter);
1409                 r = append_variant(&iter, sig, param);
1410                 if (r < 0) {
1411                         _E("append_variant error(%d)", r);
1412                         dbus_message_unref(msg);
1413                         return -EBADMSG;
1414                 }
1415         }
1416
1417         dbus_error_init(&err);
1418
1419         reply = dbus_connection_send_with_reply_and_block(conn, msg, 500, &err);
1420         dbus_message_unref(msg);
1421         if (!reply) {
1422                 _E("dbus_connection_send error(%s:%s)", err.name, err.message);
1423                 dbus_error_free(&err);
1424                 return -EBADMSG;
1425         }
1426
1427         r = dbus_message_get_args(reply, &err, DBUS_TYPE_INT32, &ret, DBUS_TYPE_INVALID);
1428         dbus_message_unref(reply);
1429         if (!r) {
1430                 _E("no message : [%s:%s]", err.name, err.message);
1431                 dbus_error_free(&err);
1432                 return -EBADMSG;
1433         }
1434
1435         return ret;
1436 }
1437
1438 #ifdef _APPFW_FEATURE_AMD_KEY
1439 static gboolean __grab_timeout_handler(gpointer data)
1440 {
1441         int pid = (int) data;
1442
1443         if(_input_window_get() != 0)
1444                 ecore_x_pointer_ungrab();
1445         _D("pid(%d) ecore_x_pointer_ungrab", pid);
1446         if(_key_ungrab(KEY_BACK) < 0) {
1447                 _W("back key ungrab error");
1448         } else {
1449                 _D("back key ungrab");
1450         }
1451
1452         return FALSE;
1453 }
1454 #endif
1455
1456 static gboolean __fg_timeout_handler(gpointer data)
1457 {
1458         struct fgmgr *fg = data;
1459
1460         if (!fg)
1461                 return FALSE;
1462
1463         _status_update_app_info_list(fg->pid, STATUS_BG, TRUE);
1464
1465         _fgmgr_list = g_list_remove(_fgmgr_list, fg);
1466         free(fg);
1467
1468         return FALSE;
1469 }
1470
1471 static void __add_fgmgr_list(int pid)
1472 {
1473         struct fgmgr *fg;
1474
1475         fg = calloc(1, sizeof(struct fgmgr));
1476         if (!fg)
1477                 return;
1478
1479         fg->pid = pid;
1480         fg->tid = g_timeout_add(5000, __fg_timeout_handler, fg);
1481
1482         _fgmgr_list = g_list_append(_fgmgr_list, fg);
1483 }
1484
1485 static void __del_fgmgr_list(int pid)
1486 {
1487         GList *iter = NULL;
1488         struct fgmgr *fg;
1489
1490         if (pid < 0)
1491                 return;
1492
1493         for (iter = _fgmgr_list; iter != NULL; iter = g_list_next(iter)) {
1494                 fg = (struct fgmgr *)iter->data;
1495                 if (fg->pid == pid) {
1496                         g_source_remove(fg->tid);
1497                         _fgmgr_list = g_list_remove(_fgmgr_list, fg);
1498                         free(fg);
1499                         return;
1500                 }
1501         }
1502 }
1503
1504 #ifdef _APPFW_FEATURE_EXPANSION_PKG_INSTALL
1505 static int __tep_mount(char *mnt_path[])
1506 {
1507         DBusMessage *msg;
1508         int func_ret = 0;
1509         int rv = 0;
1510         struct stat link_buf = {0,};
1511
1512         rv = lstat(mnt_path[0], &link_buf);
1513         if (rv == 0) {
1514                 rv = unlink(mnt_path[0]);
1515                 if (rv)
1516                         _E("Unable tp remove link file %s", mnt_path[0]);
1517         }
1518
1519         msg = dbus_message_new_method_call(TEP_BUS_NAME, TEP_OBJECT_PATH,
1520                                            TEP_INTERFACE_NAME, TEP_MOUNT_METHOD);
1521         if (!msg) {
1522                 _E("dbus_message_new_method_call(%s:%s-%s)", TEP_OBJECT_PATH,
1523                    TEP_INTERFACE_NAME, TEP_MOUNT_METHOD);
1524                 return -1;
1525         }
1526
1527         if (!dbus_message_append_args(msg,
1528                                       DBUS_TYPE_STRING, &mnt_path[0],
1529                                       DBUS_TYPE_STRING, &mnt_path[1],
1530                                       DBUS_TYPE_INVALID)) {
1531                 _E("Ran out of memory while constructing args\n");
1532                 func_ret = -1;
1533                 goto func_out;
1534         }
1535
1536         if (dbus_connection_send(conn, msg, NULL) == FALSE) {
1537                 _E("dbus_connection_send error");
1538                 func_ret = -1;
1539                 goto func_out;
1540         }
1541 func_out :
1542         dbus_message_unref(msg);
1543         return func_ret;
1544 }
1545
1546 static void __send_mount_request(const struct appinfo *ai, const char *tep_name,
1547                                  bundle *kb)
1548 {
1549         SECURE_LOGD("tep name is: %s", tep_name);
1550         char *mnt_path[2] = {NULL, };
1551         const char *installed_storage = NULL;
1552         char tep_path[PATH_MAX] = {0, };
1553
1554         const char *pkgid = appinfo_get_value(ai, AIT_PKGID);
1555         installed_storage = appinfo_get_value(ai, AIT_STORAGE_TYPE);
1556         if (installed_storage != NULL) {
1557                 SECURE_LOGD("storage: %s", installed_storage);
1558                 if (strncmp(installed_storage, "internal", 8) == 0) {
1559                         snprintf(tep_path, PATH_MAX, "%s%s/res/%s", appfw_env_get_apps_path(), pkgid,
1560                                  tep_name);
1561                         mnt_path[1] = strdup(tep_path);
1562                         snprintf(tep_path, PATH_MAX, "%s%s/res/tep", appfw_env_get_apps_path(), pkgid);
1563                         mnt_path[0] = strdup(tep_path);
1564                 } else if (strncmp(installed_storage, "external", 8) == 0) {
1565                         snprintf(tep_path, PATH_MAX, "%step/%s", appfw_env_get_external_storage_path(),
1566                                  tep_name);
1567                         mnt_path[1] = strdup(tep_path);
1568                         snprintf(tep_path, PATH_MAX, "%step/tep-access",
1569                                  appfw_env_get_external_storage_path()); /* TODO : keeping tep/tep-access for now for external storage */
1570                         mnt_path[0] = strdup(tep_path);
1571                 }
1572
1573                 if (mnt_path[0] && mnt_path[1]) {
1574                         bundle_add(kb, AUL_TEP_PATH, mnt_path[0]);
1575                         int ret = -1;
1576                         ret = aul_is_tep_mount_dbus_done(mnt_path[0]);
1577                         if (ret != 1) {
1578                                 ret = __tep_mount(mnt_path);
1579                                 if (ret < 0)
1580                                         _E("dbus error %d", ret);
1581                         }
1582                 }
1583                 if (mnt_path[0])
1584                         free(mnt_path[0]);
1585                 if (mnt_path[1])
1586                         free(mnt_path[1]);
1587         }
1588 }
1589 #endif
1590
1591 static int __check_app_control_privilege(const char *operation, int caller_pid, const struct appinfo *caller)
1592 {
1593         int ret;
1594         const char *api_ver;
1595
1596         if (caller == NULL) // daemon
1597                 return 0;
1598
1599         if (operation == NULL || caller_pid < 1)
1600                 return 0;
1601
1602         if (strcmp(operation, AUL_SVC_OPERATION_DOWNLOAD) == 0) {
1603                 api_ver = appinfo_get_value(caller, AIT_API_VER);
1604
1605                 if (!api_ver) {
1606                         _E("failed to get api version");
1607                         return -1;
1608                 }
1609
1610                 if (api_ver && strverscmp("2.4", api_ver) < 1) { // ver 2.4 or later
1611                         ret = security_server_check_privilege_by_pid(caller_pid, "privilege::tizen::download", "rw");
1612                         if (ret != SECURITY_SERVER_API_SUCCESS) {
1613                                 _E("caller %d violates http://tizen.org/privilege/download privilege (%d)", caller_pid, ret);
1614                                 return -EILLEGALACCESS;
1615                         }
1616                 }
1617         } else if (strcmp(operation, AUL_SVC_OPERATION_CALL) == 0) {
1618                 // Check the privilege for call operation
1619                 ret = security_server_check_privilege_by_pid(caller_pid, "privilege::tizen::call", "rw");
1620                 if (ret != SECURITY_SERVER_API_SUCCESS) {
1621                         _E("caller %d violates http://tizen.org/privilege/call privilege (%d)", caller_pid, ret);
1622                         return -EILLEGALACCESS;
1623                 }
1624         }
1625
1626         return 0;
1627 }
1628
1629
1630 int __check_mode(const struct appinfo *ai)
1631 {
1632 #ifdef _APPFW_FEATURE_TTS_MODE
1633         int tts_mode = 0;
1634         const char *tts_support = NULL;
1635 #endif
1636 #ifdef _APPFW_FEATURE_ULTRA_POWER_SAVING_MODE
1637         int ups_mode = 0;
1638 #endif
1639
1640 #ifdef _APPFW_FEATURE_TTS_MODE
1641         vconf_get_bool(VCONFKEY_SETAPPL_ACCESSIBILITY_TTS, &tts_mode);
1642         if(tts_mode) {
1643                 tts_support = appinfo_get_value(ai, AIT_TTS);
1644                 _W("tts : %d %s", tts_mode, tts_support);
1645                 if(strncmp(tts_support, "false", 5) == 0) {
1646                         _W("Cannot launch this app in TTS mode");
1647                         return -1;
1648                 }
1649         }
1650 #endif
1651
1652 #ifdef _APPFW_FEATURE_ULTRA_POWER_SAVING_MODE
1653         vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &ups_mode);
1654         if (ups_mode == SETTING_PSMODE_EMERGENCY) {
1655                 const char *ups_support = appinfo_get_value(ai, AIT_UPS);
1656                 _W("ups : %d %s", ups_mode, ups_support);
1657         }
1658 #endif //_APPFW_FEATURE_ULTRA_POWER_SAVING_MODE
1659
1660         return 0;
1661 }
1662
1663 static void __prepare_to_suspend_services(int pid)
1664 {
1665         int dummy;
1666         SECURE_LOGD("[__SUSPEND__] pid: %d", pid);
1667         __app_send_raw_with_noreply(pid, APP_SUSPEND, (unsigned char *)&dummy, sizeof(int));
1668 }
1669
1670 static void __prepare_to_wake_services(int pid)
1671 {
1672         int dummy;
1673         SECURE_LOGD("[__SUSPEND__] pid: %d", pid);
1674         __app_send_raw_with_noreply(pid, APP_WAKE, (unsigned char *)&dummy, sizeof(int));
1675 }
1676
1677 static gboolean __check_service_only(gpointer user_data)
1678 {
1679         int pid = GPOINTER_TO_INT(user_data);
1680         SECURE_LOGD("[__SUSPEND__] pid :%d", pid);
1681
1682         _status_check_service_only(pid, __prepare_to_suspend_services);
1683
1684         return FALSE;
1685 }
1686
1687 #ifdef _APPFW_FEATURE_PRIVATE_SERVICE
1688 static int __get_visibility_from_cert_svc(const char *pkgid, int *visibility)
1689 {
1690         int ret = 0;
1691         const char *cert_value = NULL;
1692         pkgmgrinfo_certinfo_h certinfo = NULL;
1693
1694         ret = pkgmgrinfo_pkginfo_create_certinfo(&certinfo);
1695         if (ret != 0) {
1696                 _E("pkgmgrinfo_pkginfo_create_certinfo() failed.");
1697                 return -1;
1698         }
1699
1700         ret = pkgmgrinfo_pkginfo_load_certinfo(pkgid, certinfo);
1701         if (ret != 0) {
1702                 _E("pkgmgrinfo_pkginfo_load_certinfo() failed.");
1703                 ret = -1;
1704                 goto end;
1705         }
1706
1707         ret = pkgmgrinfo_pkginfo_get_cert_value(certinfo, PMINFO_DISTRIBUTOR_ROOT_CERT,
1708                                                 &cert_value);
1709         if (ret != 0 || cert_value == NULL) {
1710                 _E("pkgmgrinfo_pkginfo_get_cert_value() failed.");
1711                 ret = -1;
1712                 goto end;
1713         }
1714
1715         ret = cert_svc_get_visibility_by_root_certificate(cert_value,
1716                 strlen(cert_value), visibility);
1717         if (ret != 0) {
1718                 _E("cert_svc_get_visibility_by_root_cert() failed. err = [%d]", ret);
1719                 ret = -1;
1720                 goto end;
1721         }
1722         _D("visibility = [%d]", *visibility);
1723
1724 end:
1725         pkgmgrinfo_pkginfo_destroy_certinfo(certinfo);
1726         return ret;
1727 }
1728 #endif
1729
1730 static int __can_share(const char *path, const char *pkgid)
1731 {
1732         struct stat path_stat;
1733
1734         if (access(path, F_OK) != 0)
1735                 return -1;
1736
1737         if (stat(path, &path_stat) != 0)
1738                 return -1;
1739
1740         if (!S_ISREG(path_stat.st_mode))
1741                 return -1;
1742
1743         char buf[1024];
1744
1745         snprintf(buf, sizeof(buf) - 1, "/opt/usr/apps/%s/data/", pkgid);
1746         if (strncmp(path, buf, strlen(buf)) != 0)
1747                 return -1;
1748
1749         return 0;
1750 }
1751
1752 static int __get_current_security_attribute(int pid, char *buf, int size)
1753 {
1754         int fd;
1755         int ret;
1756         char path[1024] = { 0, };
1757
1758         snprintf(path, sizeof(path) - 1, "/proc/%d/attr/current", pid);
1759         fd = open(path, O_RDONLY);
1760         if (fd < 0)
1761                 return -1;
1762
1763         ret = read(fd, buf, size - 1);
1764         if (ret <= 0) {
1765                 close(fd);
1766                 return -1;
1767         } else
1768                 buf[ret] = 0;
1769
1770         close(fd);
1771
1772         return 0;
1773 }
1774
1775 static int __get_exec_label_by_pid(int pid, char** exec_label)
1776 {
1777         const char *appid = NULL;
1778         const char *exec = NULL;
1779         const char *type = NULL;
1780         const struct appinfo *ai = NULL;
1781         char attr[1024] = { 0, };
1782
1783         if (__get_current_security_attribute(pid, attr, sizeof(attr)) == 0) {
1784                 *exec_label = strdup(attr);
1785                 return 0;
1786         }
1787
1788         appid = _status_app_get_appid_bypid(pid);
1789         if (appid) {
1790                 ai = appinfo_find(_laf, appid);
1791                 exec = appinfo_get_value(ai, AIT_EXEC);
1792                 type = appinfo_get_value(ai, AIT_TYPE);
1793
1794                 if (exec && type) {
1795                         if (strcmp("wgt", type) == 0) {
1796                                 if (smack_lgetlabel(exec, exec_label, SMACK_LABEL_EXEC) == 0)
1797                                         return 0;
1798                         } else {
1799                                 if (smack_getlabel(exec, exec_label, SMACK_LABEL_EXEC) == 0)
1800                                         return 0;
1801                         }
1802                 }
1803         }
1804
1805         return -1;
1806 }
1807
1808 static int __get_exec_label_by_appid(const char *appid, char** exec_label)
1809 {
1810         const char *exec = NULL;
1811         const char *type = NULL;
1812         const struct appinfo *ai = NULL;
1813
1814         if (appid) {
1815                 ai = appinfo_find(_laf, appid);
1816                 exec = appinfo_get_value(ai, AIT_EXEC);
1817                 type = appinfo_get_value(ai, AIT_TYPE);
1818
1819                 if (exec && type) {
1820                         if (strcmp("wgt", type) == 0) {
1821                                 if (smack_lgetlabel(exec, exec_label, SMACK_LABEL_EXEC) == 0)
1822                                         return 0;
1823                         } else {
1824                                 if (smack_getlabel(exec, exec_label, SMACK_LABEL_EXEC) == 0)
1825                                         return 0;
1826                         }
1827                 }
1828         }
1829
1830         return -1;
1831 }
1832
1833 static int __get_owner_pid(int caller_pid, bundle *kb)
1834 {
1835         char *org_caller = NULL;
1836
1837         if (bundle_get_str(kb, AUL_K_ORG_CALLER_PID, &org_caller) == BUNDLE_ERROR_NONE) {
1838                 int org_caller_pid = atoi(org_caller);
1839                 char *c_exec_label = NULL;
1840
1841                 if (__get_exec_label_by_pid(caller_pid, &c_exec_label) == 0) {
1842
1843                         if (c_exec_label &&
1844                                 (strcmp(APP_SELECTOR, c_exec_label) == 0 ||
1845                                 strcmp(SHARE_PANEL, c_exec_label) == 0))
1846                                         caller_pid = org_caller_pid;
1847                 }
1848
1849                 if (c_exec_label)
1850                         free(c_exec_label);
1851         }
1852
1853         return caller_pid;
1854 }
1855
1856 static int __get_exec_label(char **caller_exec_label, char **callee_exec_label,
1857                                 int caller_pid, const char *appid)
1858 {
1859         char *label_caller = NULL;
1860         char *label = NULL;
1861
1862         if (__get_exec_label_by_pid(caller_pid, &label_caller) != 0) {
1863                 return -1;
1864         }
1865
1866         if (__get_exec_label_by_appid(appid, &label) != 0) {
1867                 free(label_caller);
1868                 return -1;
1869         }
1870
1871         *caller_exec_label = label_caller;
1872         *callee_exec_label = label;
1873         return 0;
1874 }
1875
1876 static int __grant_temporary_permission(int caller_pid, const char *appid, bundle *kb,
1877                                         char **out_caller_exec_label, char **out_callee_exec_label,
1878                                         char ***out_paths)
1879 {
1880         int type = bundle_get_type(kb, AUL_SVC_DATA_PATH);
1881         char *path = NULL;
1882         const char **path_array = NULL;
1883         int len;
1884         char *caller_exec_label = NULL;
1885         char *callee_exec_label = NULL;
1886         int i;
1887         char **paths = NULL;
1888         int ret = -1;
1889         int owner_pid = -1;
1890         const char *tmp_appid = NULL;
1891         const char *pkgid = NULL;
1892         const struct appinfo *ai = NULL;
1893
1894         switch (type) {
1895                 case BUNDLE_TYPE_STR:
1896                         bundle_get_str(kb, AUL_SVC_DATA_PATH, &path);
1897
1898                         if (!path) {
1899                                 _E("path was null");
1900                                 goto check_uri;
1901                         }
1902
1903                         owner_pid = __get_owner_pid(caller_pid, kb);
1904                         owner_pid = getpgid(owner_pid); /* for webapp */
1905                         tmp_appid = _status_app_get_appid_bypid(owner_pid);
1906
1907                         ai = appinfo_find(_laf, tmp_appid);
1908                         pkgid = appinfo_get_value(ai, AIT_PKGID);
1909
1910                         if (__can_share(path, pkgid) != 0) {
1911                                 _E("__can_share() returned an error");
1912                                 goto check_uri;
1913                         }
1914
1915                         if (__get_exec_label(&caller_exec_label, &callee_exec_label, owner_pid,
1916                                 appid) != 0) {
1917                                 _E("__get_exec_label() returned an error");
1918                                 goto finally;
1919                         }
1920
1921                         paths = (char**)malloc(sizeof(char*) * 3);
1922                         if (!paths) {
1923                                 _E("Out of memory");
1924                                 goto finally;
1925                         }
1926
1927                         paths[0] = strdup(path);
1928                         paths[1] = NULL;
1929                         paths[2] = NULL;
1930                         ret = 0;
1931                         break;
1932
1933                 case BUNDLE_TYPE_STR_ARRAY:
1934                         path_array = bundle_get_str_array(kb, AUL_SVC_DATA_PATH, &len);
1935                         if (!path_array || len <= 0) {
1936                                 _E("path_array was null");
1937                                 goto check_uri;
1938                         }
1939
1940                         owner_pid = __get_owner_pid(caller_pid, kb);
1941                         owner_pid = getpgid(owner_pid); /* for webapp */
1942                         tmp_appid = _status_app_get_appid_bypid(owner_pid);
1943                         ai = appinfo_find(_laf, tmp_appid);
1944                         pkgid = appinfo_get_value(ai, AIT_PKGID);
1945
1946                         if (__get_exec_label(&caller_exec_label, &callee_exec_label, owner_pid,
1947                                 appid) != 0) {
1948                                 _E("__get_exec_label() returned an error");
1949                                 goto finally;
1950                         }
1951
1952                         paths = (char**)malloc(sizeof(char*) * (len + 2));
1953                         if (!paths) {
1954                                 _E("Out of memory");
1955                                 goto finally;
1956                         }
1957
1958                         int cnt = 0;
1959                         for (i = 0; i < len; i++) {
1960                                 if (__can_share(path_array[i], pkgid) == 0) {
1961                                         paths[cnt++] = strdup(path_array[i]);
1962                                 }
1963                         }
1964                         if (cnt > 0){
1965                                 paths[cnt] = NULL;
1966                                 paths[cnt + 1] = NULL;
1967                                 ret = 0;
1968                         } else {
1969                                 free(paths);
1970                                 paths = NULL;
1971                         }
1972                         break;
1973         }
1974
1975 check_uri:
1976         if (bundle_get_str(kb, AUL_SVC_K_URI, &path) == BUNDLE_ERROR_NONE) {
1977                 if (!path) {
1978                         _E("path was null");
1979                         goto finally;
1980                 }
1981
1982                 if (strncmp(path, "file://", 7) == 0)
1983                         path = &path[7];
1984                 else {
1985                         _E("file wasn't started with file://");
1986                         goto finally;
1987                 }
1988
1989                 if (owner_pid == -1) {
1990                         owner_pid = __get_owner_pid(caller_pid, kb);
1991                         owner_pid = getpgid(owner_pid); /* for webapp */
1992                 }
1993
1994                 tmp_appid = _status_app_get_appid_bypid(owner_pid);
1995                 ai = appinfo_find(_laf, tmp_appid);
1996                 pkgid = appinfo_get_value(ai, AIT_PKGID);
1997
1998                 if (__can_share(path, pkgid) != 0) {
1999                         _E("__can_share() returned an error");
2000                         goto finally;
2001                 }
2002
2003                 if (!caller_exec_label && !callee_exec_label)
2004                         if (__get_exec_label(&caller_exec_label, &callee_exec_label, owner_pid,
2005                                 appid) != 0) {
2006                                 _E("__get_exec_label() returned an error");
2007                                 goto finally;
2008                         }
2009
2010                 if (!paths) {
2011                         paths = (char**)malloc(sizeof(char*) * 2);
2012                         if (!paths) {
2013                                 _E("Out of memory");
2014                                 goto finally;
2015                         }
2016
2017                         paths[0] = strdup(path);
2018                         paths[1] = NULL;
2019                 } else {
2020                         i = 0;
2021                         while (1) {
2022                                 if (paths[i] == NULL) {
2023                                         paths[i] = strdup(path);
2024                                         break;
2025                                 }
2026                                 i++;
2027                         }
2028                 }
2029                 ret = 0;
2030         }
2031 finally:
2032         if (ret == 0 && caller_exec_label && paths) {
2033                 _D("grant permission %s : %s : %s", paths[0], caller_exec_label,
2034                         callee_exec_label);
2035                 int r = security_server_perm_apply_sharing(NULL, (const char**)paths, caller_exec_label, callee_exec_label);
2036                 if (r != SECURITY_SERVER_API_SUCCESS) {
2037                         _E("security_server_perm_apply_sharing() returned an error %d",r);
2038                         ret = -1;
2039                 } else {
2040                         *out_caller_exec_label = caller_exec_label;
2041                         *out_callee_exec_label = callee_exec_label;
2042                         *out_paths = paths;
2043
2044                         caller_exec_label = NULL;
2045                         callee_exec_label = NULL;
2046                         paths = NULL;
2047                 }
2048         }
2049
2050         if (caller_exec_label)
2051                 free(caller_exec_label);
2052         if (callee_exec_label)
2053                 free(callee_exec_label);
2054         if (paths) {
2055                 i = 0;
2056                 while (1) {
2057                         if (paths[i] == NULL) {
2058                                 free(paths);
2059                                 break;
2060                         }
2061                         free(paths[i]);
2062                         i++;
2063                 }
2064         }
2065         return ret;
2066 }
2067
2068 static void __add_shared_info(int pid, const char *caller_exec_label, const char *callee_exec_label, char **paths)
2069 {
2070         _status_set_exec_label(pid, callee_exec_label);
2071         _status_add_shared_info(pid, caller_exec_label, paths);
2072 }
2073
2074 #ifdef _APPFW_FEATURE_TERMINATE_UNMANAGEABLE_APP
2075 #ifdef _APPFW_FEATURE_SEND_HOME_LAUNCH_SIGNAL
2076 static void __check_home_app(int pid)
2077 {
2078         const char *home_app = _get_home_appid();
2079         const char *appid = NULL;
2080
2081         if (home_app == NULL)
2082                 return;
2083
2084         appid = _status_app_get_appid_bypid(pid);
2085
2086         if (appid && strcmp(appid, home_app) == 0) {
2087                 int cnt = 0;
2088                 int *pids = NULL;
2089                 int i;
2090                 const char *taskmanage = NULL;
2091                 const struct appinfo *ai = NULL;
2092                 const char* bg = NULL;
2093
2094                 app_group_get_leader_pids(&cnt, &pids);
2095                 if (pids) {
2096                         for (i = 0; i < cnt; ++i) {
2097                                 appid = _status_app_get_appid_bypid(pids[i]);
2098                                 if (appid == NULL || strcmp(appid, home_app) == 0)
2099                                         continue;
2100
2101                                 ai = appinfo_find(_laf, appid);
2102                                 taskmanage = appinfo_get_value(ai, AIT_TASKMANAGE);
2103                                 bg = appinfo_get_value(ai, AIT_ALLOWED_BG);
2104
2105                                 if (taskmanage && strcmp("false", taskmanage) == 0
2106                                         && bg && strcmp("NONE", bg) == 0) {
2107                                         int st = _status_get_app_info_status(pids[i]);
2108                                         if (st != STATUS_DYING) {
2109                                                 _W("terminate %d %s %d", pids[i], appid, st);
2110                                                 aul_update_freezer_status(pids[i], "wakeup");
2111                                                 _term_sub_app(pids[i]);
2112                                         }
2113                                 }
2114                         }
2115
2116                         free(pids);
2117                 }
2118         }
2119 }
2120 #endif
2121 #endif
2122
2123 int _revoke_temporary_permission(int pid)
2124 {
2125         GList *list = _status_get_shared_info_list(pid);
2126         const char *callee_label = _status_get_exec_label(pid);
2127
2128         if (!list || !callee_label) {
2129                 _E("list or callee_label was null");
2130                 return -1;
2131         }
2132
2133         while (list) {
2134                 shared_info_t *sit = (shared_info_t*)list->data;
2135
2136                 _D("revoke permission %s : %s", sit->owner_exec_label, callee_label);
2137                 int r = security_server_perm_drop_sharing(NULL, (const char**)sit->paths,
2138                                 sit->owner_exec_label, callee_label);
2139
2140                 if (r != SECURITY_SERVER_API_SUCCESS)
2141                         _E("revoke error %d",r);
2142
2143                 list = g_list_next(list);
2144         }
2145         return _status_clear_shared_info_list(pid);
2146 }
2147
2148 int _start_app(const char* appid, bundle* kb, int cmd, int caller_pid, uid_t caller_uid, int fd)
2149 {
2150         const struct appinfo *ai;
2151         const struct appinfo *caller_ai;
2152         int ret = -1;
2153         const char *multiple = NULL;
2154         const char *app_path = NULL;
2155         const char *pkg_type = NULL;
2156         const char *component_type = NULL;
2157         int pid = -1;
2158         char tmp_pid[MAX_PID_STR_BUFSZ];
2159         const char *permission;
2160         const char *pkgid;
2161         const char *preload;
2162         const char *pkg_status;
2163         const char *operation;
2164         char caller_appid[256] = {0,};
2165         char* caller = NULL;
2166         char* old_caller = NULL;
2167         pkgmgrinfo_cert_compare_result_type_e compare_result;
2168         int delay_reply = 0;
2169         int pad_pid = NO_LAUNCHPAD_PID;
2170         int status = -1;
2171         int r = -1;
2172         char trm_buf[MAX_PACKAGE_STR_SIZE];
2173         gboolean is_group_app = FALSE;
2174 #ifdef _APPFW_FEATURE_AMD_MODULE_LOG
2175         char log_status[AUL_PR_NAME] = {0,};
2176 #endif
2177
2178 #ifdef _APPFW_FEATURE_FAKE_EFFECT
2179         const char *fake_effect;
2180         int effect_mode = 0;
2181 #ifdef _APPFW_FEATURE_WMS_CONNECTION_CHECK
2182         int wmanager_connected = 0;
2183 #endif
2184
2185 #endif /* _APPFW_FEATURE_FAKE_EFFECT */
2186
2187 #ifdef _APPFW_FEATURE_AMD_KEY
2188         const char *caller_component_type = NULL;
2189 #endif
2190         int prelaunch_attribute = 0;
2191
2192         bool bg_allowed = false;
2193         int lpid;
2194         gboolean can_attach;
2195         app_group_launch_mode launch_mode;
2196         char *caller_exec_label = NULL;
2197         char *callee_exec_label = NULL;
2198         char **paths = NULL;
2199         int grant_permission = 0;
2200
2201         traceBegin(TTRACE_TAG_APPLICATION_MANAGER, "AUL:AMD:START_APP");
2202         if (appid == NULL || kb == NULL
2203                 || caller_pid < 0 || fd < 0) {
2204                 _D("invalid parameter");
2205                 if (fd >= 0)
2206                         __real_send(fd, -1);
2207                 traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
2208                 return -1;
2209         }
2210
2211         snprintf(tmp_pid, MAX_PID_STR_BUFSZ, "%d", caller_pid);
2212         bundle_add(kb, AUL_K_CALLER_PID, tmp_pid);
2213
2214         if (cmd == APP_START_RES)
2215                 bundle_add(kb, AUL_K_WAIT_RESULT, "1");
2216
2217         caller = _status_app_get_appid_bypid(caller_pid);
2218         if(caller == NULL) {
2219                 ret = aul_app_get_appid_bypid(caller_pid, caller_appid, sizeof(caller_appid));
2220                 if(ret == 0) {
2221                         bundle_add(kb, AUL_K_CALLER_APPID, caller_appid);
2222                         caller = caller_appid;
2223                 } else {
2224                         _E("no caller appid info, ret: %d", ret);
2225                 }
2226         } else {
2227                 bundle_add(kb, AUL_K_CALLER_APPID, caller);
2228         }
2229
2230         if (caller)
2231                 SECURE_LOGW("caller appid : %s", caller);
2232
2233         _W("caller pid : %d", caller_pid);
2234
2235         ai = appinfo_find(_laf, appid);
2236         if(ai == NULL) {
2237                 _E("no appinfo");
2238                 __real_send(fd, -ENOAPP);
2239                 traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
2240                 return -1;
2241         } else {
2242                 pkg_status = appinfo_get_value(ai, AIT_STATUS);
2243                 if(pkg_status && (strncmp(pkg_status, "blocking", 8) == 0 || strncmp(pkg_status, "restart", 7) == 0) ) {
2244                         _D("blocking");
2245                         __real_send(fd, -EREJECTED);
2246                         traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
2247                         return -EREJECTED;
2248                 } else if(pkg_status && strncmp(pkg_status, "unmounted", 9) == 0 ) {
2249                         _D("unmounted");
2250                         __real_send(fd, -1);
2251                         traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
2252                         return -1;
2253                 }
2254         }
2255
2256         app_path = appinfo_get_value(ai, AIT_EXEC);
2257         pkg_type = appinfo_get_value(ai, AIT_TYPE);
2258         permission = appinfo_get_value(ai, AIT_PERM);
2259         pkgid = appinfo_get_value(ai, AIT_PKGID);
2260         component_type = appinfo_get_value(ai, AIT_COMPTYPE);
2261
2262         if (pkg_type == NULL)
2263                 pkg_type = "unknown";
2264
2265 #ifdef _APPFW_FEATURE_EXPANSION_PKG_INSTALL
2266         const char *tep_name = NULL;
2267
2268         tep_name = appinfo_get_value(ai, AIT_TEP);
2269         if (tep_name != NULL) {
2270                 __send_mount_request(ai, tep_name, kb);
2271         }
2272 #endif
2273         operation = bundle_get_val(kb, "__APP_SVC_OP_TYPE__");
2274         caller_ai = appinfo_find(_laf, caller);
2275 #ifdef _APPFW_FEATURE_AMD_KEY
2276         caller_component_type = appinfo_get_value(caller_ai, AIT_COMPTYPE);
2277 #endif
2278
2279         if(permission && strncmp(permission, "signature", 9) == 0 ) {
2280                 if(caller_uid != 0 && (cmd == APP_START
2281                                         || cmd == APP_START_RES
2282                                         || cmd == APP_START_ASYNC
2283 #ifdef _APPFW_FEATURE_MULTI_INSTANCE
2284                                         || cmd == APP_START_MULTI_INSTANCE
2285 #endif
2286                                         ))
2287                 {
2288                         preload = appinfo_get_value(caller_ai, AIT_PRELOAD);
2289                         if( preload && strncmp(preload, "true", 4) != 0 ) {
2290                                 ret = pkgmgrinfo_pkginfo_compare_app_cert_info(caller, appid, &compare_result);
2291                                 if (ret != PMINFO_R_OK) {
2292                                         pid = -1;
2293                                         _E("compare app cert info failed : %d", ret);
2294                                         if(cmd == APP_START_ASYNC)
2295                                                 close(fd);
2296                                         else
2297                                                 __real_send(fd, pid);
2298                                         traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
2299                                         return pid;
2300                                 }
2301                                 if(compare_result != PMINFO_CERT_COMPARE_MATCH) {
2302                                         pid = -EILLEGALACCESS;
2303                                         if(cmd == APP_START_ASYNC)
2304                                                 close(fd);
2305                                         else
2306                                                 __real_send(fd, pid);
2307                                         traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
2308                                         return pid;
2309                                 }
2310                         }
2311                 }
2312         }
2313
2314         if(operation && caller_ai) {
2315                 ret = __check_app_control_privilege(operation, caller_pid, caller_ai);
2316                 if (ret < 0) {
2317                         if (cmd == APP_START_ASYNC)
2318                                 close(fd);
2319                         else
2320                                 __real_send(fd, ret);
2321                         traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
2322                         return ret;
2323                 }
2324         }
2325
2326         if(__check_mode(ai) < 0) {
2327                 pid = -EREJECTED;
2328                 if(cmd == APP_START_ASYNC)
2329                         close(fd);
2330                 else
2331                         __real_send(fd, pid);
2332                 traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
2333                 return pid;
2334         }
2335
2336         pkgmgrinfo_client_request_enable_external_pkg((char *)pkgid);
2337
2338         if (component_type && (strncmp(component_type, APP_TYPE_UI, strlen(APP_TYPE_UI)) == 0
2339                 || strncmp(component_type, APP_TYPE_WATCH, strlen(APP_TYPE_WATCH)) == 0
2340                 || strncmp(component_type, APP_TYPE_WIDGET, strlen(APP_TYPE_WIDGET)) == 0 )) {
2341                 gboolean new_process = FALSE;
2342
2343                 multiple = appinfo_get_value(ai, AIT_MULTI);
2344                 if (!multiple || strncmp(multiple, "false", 5) == 0) {
2345                         pid = _status_app_is_running_v2(appid);
2346                 } else if (operation != NULL && strncmp(operation, "http://tizen.org/appcontrol/operation/view", 512) == 0){
2347                         old_caller = _status_get_caller_by_appid(appid);
2348                         if(old_caller && caller) {
2349                                 if(strncmp(old_caller, caller, MAX_PACKAGE_STR_SIZE) == 0) {
2350                                         pid = _status_app_is_running_v2(appid);
2351                                 }
2352                         }
2353                 }
2354
2355                 if (strncmp(component_type, APP_TYPE_UI, strlen(APP_TYPE_UI)) == 0) {
2356                         if (app_group_is_group_app(kb)) {
2357                                 pid = -1;
2358                                 is_group_app = TRUE;
2359                         }
2360
2361                         int st = -1;
2362
2363                         if (pid > 0)
2364                                 st = _status_get_app_info_status(pid);
2365
2366                         if (pid == -1 || st == STATUS_DYING) {
2367                                 int found_pid = -1;
2368                                 int found_lpid = -1;
2369
2370                                 if (app_group_find_singleton(appid, &found_pid, &found_lpid) == 0) {
2371                                         pid = found_pid;
2372                                         new_process = FALSE;
2373                                 } else {
2374                                         new_process = TRUE;
2375                                 }
2376
2377                                 if (app_group_can_start_app(appid, kb, &can_attach, &lpid, &launch_mode) != 0 ) {
2378                                         _E("can't make group info");
2379                                         pid = -EILLEGALACCESS;
2380                                         if (cmd == APP_START_ASYNC)
2381                                                 close(fd);
2382                                         else
2383                                                 __real_send(fd, pid);
2384                                         traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
2385                                         return pid;
2386                                 }
2387
2388                                 if (can_attach && lpid == found_lpid) {
2389                                         _E("can't launch singleton app in the same group");
2390                                         pid = -EILLEGALACCESS;
2391                                         if (cmd == APP_START_ASYNC)
2392                                                 close(fd);
2393                                         else
2394                                                 __real_send(fd, pid);
2395                                         traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
2396                                         return pid;
2397                                 }
2398
2399                                 if (found_pid != -1) {
2400                                         _W("app_group_clear_top, pid: %d", found_pid);
2401                                         app_group_clear_top(found_pid);
2402                                 }
2403                         }
2404
2405                         if (pid == -1 && can_attach)
2406                                 pid = app_group_find_pid_from_recycle_bin(appid);
2407                 }
2408
2409 #ifdef _APPFW_FEATURE_APP_CONTROL_LITE
2410                 char app_path_link[512] = {0,};
2411                 const char *caller_app_path = NULL;
2412                 char caller_app_path_link[512] = {0,};
2413
2414                 caller_app_path = appinfo_get_value(caller_ai, AIT_EXEC);
2415
2416                 SECURE_LOGD("callee path(%s) caller path(%s)", app_path, caller_app_path);
2417
2418                 readlink(app_path, app_path_link, 512);
2419                 readlink(caller_app_path, caller_app_path_link, 512);
2420
2421                 SECURE_LOGD("callee link(%s) caller link(%s)", app_path_link, caller_app_path_link);
2422
2423                 if(strncmp(app_path_link, "/usr/bin/ug-client", 512) == 0) {
2424                         if (strcmp(caller, "org.tizen.app-selector") == 0){
2425                                 pid = atoi(bundle_get_val(kb,AUL_K_ORG_CALLER_PID));
2426                                 bundle_add(kb, "__AUL_UG_EXEC__", app_path);
2427                                 SECURE_LOGD("app_path : %s , ug id(%s)", app_path, bundle_get_val(kb, "__AUL_UG_ID__"));
2428                         } else if(strncmp(caller_app_path_link, "/usr/bin/ug-client", 512) == 0) {
2429                                 __real_send(fd, -EUGLOCAL_LAUNCH);
2430                                 traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
2431                                 return -1;
2432                         }
2433                 }
2434 #endif
2435
2436                 if(caller) {
2437 #ifdef _APPFW_FEATURE_AMD_KEY
2438                         if (caller_component_type && strncmp(caller_component_type, APP_TYPE_UI, strlen(APP_TYPE_UI)) == 0) {
2439                                 Ecore_X_Window in_win;
2440                                 in_win = _input_window_get();
2441                                 if(in_win) {
2442                                         ret = ecore_x_pointer_grab(in_win);
2443                                         _D("win(%x) ecore_x_pointer_grab(%d)", in_win, ret);
2444                                 }
2445                                 if(_key_grab(KEY_BACK, EXCLUSIVE_GRAB) < 0) {
2446                                         _W("back key grab error");
2447                                 } else {
2448                                         _D("back key grab");
2449                                 }
2450                         }
2451 #endif
2452                 }
2453
2454                 if (__grant_temporary_permission(caller_pid, appid, kb,
2455                         &caller_exec_label, &callee_exec_label, &paths) == 0)
2456                         grant_permission = 1;
2457
2458                 status = _status_get_app_info_status(pid);
2459                 if (pid > 0 && status != STATUS_DYING) {
2460                         if (caller_pid == pid) {
2461                                 SECURE_LOGD("caller process & callee process is same.[%s:%d]", appid, pid);
2462                                 pid = -ELOCALLAUNCH_ID;
2463                         } else {
2464                                 if(strncmp(pkg_type, "wgt", 3) == 0) {
2465                                         __pre_launching_processing(appid);
2466                                 }
2467
2468 #ifdef _APPFW_FEATURE_AMD_MODULE_LOG
2469                         strncpy(log_status, "RESUMING", strlen("RESUMING"));
2470 #endif
2471
2472                                 proc_group_change_status(PROC_CGROUP_SET_RESUME_REQUEST, pid, (char *)appid);
2473
2474                                 if ((ret = __nofork_processing(cmd, pid, kb, fd)) < 0) {
2475                                         pid = ret;
2476                                 } else {
2477                                         delay_reply = 1;
2478                                 }
2479                         }
2480                 } else if (cmd != APP_RESUME) {
2481                         if(status == STATUS_DYING && pid > 0) {
2482                                 r = kill(pid, SIGKILL);
2483                                 if (r == -1)
2484                                         _D("send SIGKILL: %s", strerror(errno));
2485                         }
2486
2487 #ifdef _APPFW_FEATURE_AMD_MODULE_LOG
2488                         strncpy(log_status, "LAUNCHING", strlen("LAUNCHING"));
2489 #endif
2490
2491                         // 2.4 bg categorized ui app || watch || widget -> bg allowed (2.3 ui app -> not allowed)
2492                         prelaunch_attribute = __get_prelaunch_attribute(ai);
2493                         if ((strncmp(component_type, APP_TYPE_UI, sizeof(APP_TYPE_UI)) == 0
2494                                 && (prelaunch_attribute & RESOURCED_ALLOWED_BG_ATTRIBUTE))
2495                                         || (strncmp(component_type, APP_TYPE_WATCH, sizeof(APP_TYPE_WATCH)) == 0)
2496                                         || (strncmp(component_type, APP_TYPE_WIDGET, sizeof(APP_TYPE_WIDGET)) == 0)) {
2497                                 _D("[__SUSPEND__] allowed background, appid: %s", appid);
2498                                 bundle_add(kb, AUL_K_ALLOWED_BG, "ALLOWED_BG");
2499                         }
2500
2501                         __send_proc_prelaunch_signal(appid, pkgid, prelaunch_attribute);
2502
2503 #ifdef _APPFW_FEATURE_FAKE_EFFECT
2504                         fake_effect = bundle_get_val(kb, "__FAKE_EFFECT__");
2505 #endif
2506
2507 #ifdef _APPFW_FEATURE_CPU_BOOST
2508                         if (cmd == APP_OPEN || operation != NULL ||
2509                                 (caller != NULL && strcmp(caller, "org.tizen.wnotification2") == 0) ||
2510                                 (caller != NULL && strcmp(caller, "org.tizen.wnotiboard-popup") == 0)) {
2511                                 char *arr[2];
2512                                 char val[32];
2513                                 snprintf(val, sizeof(val), "%d", APP_BOOSTING_PERIOD);
2514                                 arr[0] = val;
2515                                 arr[1] = NULL;
2516                                 ret = invoke_dbus_method_sync(SYSTEM_BUS_NAME, SYSTEM_OBJECT_PATH,
2517                                                 SYSTEM_INTERFACE_NAME, SYSTEM_METHOD_NAME, "i", arr);
2518                                 _D("%s-%s : %d", SYSTEM_INTERFACE_NAME, SYSTEM_METHOD_NAME, ret);
2519                         }
2520 #endif
2521
2522 #ifdef _APPFW_FEATURE_FAKE_EFFECT
2523                         /*
2524                          *      effect_mode = 0
2525                          *              default mode : fake effect off, 1.6 MHZ off
2526                          *      effect_mode = 1
2527                          *              full mode : fake effect on, 1.6 MHZ on
2528                          *      effect_mode = 2
2529                          *              fake effect mode : fake effect on, 1.6 MHZ off
2530                          *      effect_mode = 3
2531                          *              1.6 MHZ mode : faek effect off, 1.6MHZ on
2532                          *
2533                          */
2534                         vconf_get_int(VCONFKEY_AMD_EFFECT_IMAGE_ENABLE, &effect_mode);
2535 #ifdef _APPFW_FEATURE_WMS_CONNECTION_CHECK
2536                         vconf_get_bool(VCONFKEY_WMS_WMANAGER_CONNECTED, &wmanager_connected);
2537 #endif
2538                         //_D("*******[effect_mode] Mode : %d\n ", effect_mode );
2539
2540                         if ( ( cmd == APP_OPEN ||
2541                                 ( (operation != NULL && strncmp(operation, "http://tizen.org/appcontrol/operation/main", 512) == 0)
2542                                 && !(fake_effect != NULL && strncmp(fake_effect, "OFF", 3) == 0) )
2543                                 ) && (effect_mode == 1 || effect_mode == 2)
2544 #ifdef _APPFW_FEATURE_WMS_CONNECTION_CHECK
2545                                 && wmanager_connected == true
2546 #endif
2547                                 ){
2548                                 char image_filename[256] = {0,};
2549                                 const char *portraitimg = NULL;
2550                                 const char *landscapeimg = NULL;
2551 #ifdef _APPFW_FEATURE_CHANGEABLE_COLOR
2552                                 const char *effectimg_type = NULL;
2553 #endif
2554                                 const char *indicator = NULL;
2555                                 int screen_mode = 0;
2556                                 int rotate_allowed = 0;
2557                                 int file_type = 1;
2558                                 int theme = 0;
2559
2560                                 //vconf_get_int(PHONE_ORIENTATION_MODE, &screen_mode);
2561                                 screen_mode = invoke_dbus_method_sync(ROTATION_BUS_NAME, ROTATION_OBJECT_PATH,
2562                                                                 ROTATION_INTERFACE_NAME, ROTATION_METHOD_NAME, "i", NULL);
2563                                 _D("%s-%s : %d", ROTATION_INTERFACE_NAME, ROTATION_METHOD_NAME, screen_mode);
2564                                 vconf_get_bool(PHONE_ROTATE_LOCK, &rotate_allowed); /*TODO: use vconf_notify_key_changed()*/
2565                                 portraitimg = appinfo_get_value(ai, AIT_EFFECTIMAGEPORT);
2566                                 landscapeimg = appinfo_get_value(ai, AIT_EFFECTIMAGELAND);
2567 #ifdef _APPFW_FEATURE_CHANGEABLE_COLOR
2568                                 effectimg_type = appinfo_get_value(ai, AIT_EFFECTTYPE);
2569 #endif
2570                                 indicator = appinfo_get_value(ai, AIT_INDICATOR_DISP);
2571                                 /*Currently we assume apps supporting launching effect supports portrait mode*/
2572                                 if (indicator && portraitimg) {
2573                                         if (rotate_allowed == false) {
2574                                                 screen_mode = 1;
2575                                         }
2576                                         if ((screen_mode == 2 || screen_mode == 4) && (rotate_allowed == true)) {
2577                                                 /*if there is no landscape image, that means app supports only portrait mode.*/
2578                                                 if (landscapeimg) {
2579                                                         snprintf(image_filename, 255, "%s", landscapeimg);
2580                                                 }
2581                                         } else {
2582                                                 snprintf(image_filename, 255, "%s", portraitimg);
2583                                         }
2584                                         if (access(image_filename, R_OK) == 0) {
2585 #ifdef _APPFW_FEATURE_CHANGEABLE_COLOR
2586                                                 if(strncmp(effectimg_type, "edj-dark", strlen(effectimg_type)) == 0) {
2587                                                         file_type = 0;
2588                                                         theme = 0;
2589                                                 } else if (strncmp(effectimg_type, "edj-light", strlen(effectimg_type)) == 0) {
2590                                                         file_type = 0;
2591                                                         theme = 1;
2592                                                 } else if (strncmp(effectimg_type, "edj-default", strlen(effectimg_type)) == 0) {
2593                                                         file_type = 0;
2594                                                         theme = 2;
2595                                                 } else {
2596                                                         file_type = 1;
2597                                                         theme = 0;
2598                                                 }
2599
2600                                                 do {
2601                                                         if (file_type == 1)
2602                                                                 break;
2603                                                         r = snprintf(xml_filename, 255, "/usr/apps/%s/shared/res/tables/%s_ChangeableColorInfo.xml", pkgid, pkgid);
2604                                                         if (access(xml_filename, R_OK) == 0) {
2605                                                                 //snprintf(image_filename, 255, "%s:%s", image_filename, xml_filename);
2606                                                                 strcat(image_filename, ":");
2607                                                                 strcat(image_filename, xml_filename);
2608                                                                 break;
2609                                                         }
2610                                                         r = snprintf(xml_filename, 255, "/opt/usr/apps/%s/shared/res/tables/%s_ChangeableColorInfo.xml", pkgid, pkgid);
2611                                                         if (access(xml_filename, R_OK) == 0) {
2612                                                                 //snprintf(image_filename, 255, "%s:%s", image_filename, xml_filename);
2613                                                                 strcat(image_filename, ":");
2614                                                                 strcat(image_filename, xml_filename);
2615                                                                 break;
2616                                                         }
2617                                                 } while(0);
2618 #endif
2619 #ifndef _APPFW_FEATURE_DEFAULT_FAKE_IMAGE
2620                                                 __amd_effect_image_file_set(image_filename);
2621 #else
2622                                                 if(file_type == 1) {
2623                                                         __amd_effect_image_file_set("/usr/share/splash_images/type0_portrait.bmp");
2624                                                 } else {
2625                                                         __amd_effect_image_file_set(image_filename);
2626                                                 }
2627 #endif
2628                                                 __amd_send_message_to_e17(screen_mode, indicator, file_type, theme);
2629                                         }
2630                                 }
2631                         }
2632 #endif /* _APPFW_FEATURE_FAKE_EFFECT */
2633
2634 #ifdef _APPFW_FEATURE_DEBUG_LAUNCHPAD
2635                         if (bundle_get_type(kb, AUL_K_SDK) != BUNDLE_TYPE_NONE) {
2636                                 pad_pid = DEBUG_LAUNCHPAD_PID;
2637                         } else if (strncmp(pkg_type, "wgt", 3) == 0) {
2638                                 pad_pid = WEB_LAUNCHPAD_PID;
2639                         }
2640 #else
2641                         if (strncmp(pkg_type, "wgt", 3) == 0) {
2642                                 pad_pid = WEB_LAUNCHPAD_PID;
2643                         }
2644 #endif
2645 #ifdef _APPFW_FEATURE_PROCESS_POOL
2646                         else {
2647                                 const char *process_pool = appinfo_get_value(ai, AIT_POOL);
2648                                 _D("process_pool: %s", process_pool);
2649
2650                                 const char *hwacc = appinfo_get_value(ai, AIT_HWACC);
2651                                 _D("h/w acceleration: %s", hwacc);
2652
2653                                 SECURE_LOGD("appid: %s", appid);
2654
2655                                 pad_pid = PROCESS_POOL_LAUNCHPAD_PID;
2656                                 char pad_sock[UNIX_PATH_MAX] = { 0, };
2657                                 snprintf(pad_sock, UNIX_PATH_MAX, "%s/%d", AUL_SOCK_PREFIX, pad_pid);
2658                                 if (access(pad_sock, F_OK) != 0)
2659                                 {
2660                                         _W("Sending to legacy launchpad because process-pool launchpad is not initialized.");
2661                                         pad_pid = NO_LAUNCHPAD_PID;
2662                                 }
2663                         }
2664 #endif //_APPFW_FEATURE_PROCESS_POOL
2665                         _W("pad pid(%d)", pad_pid);
2666                         __set_appinfo_for_launchpad(ai, kb);
2667                         if (pad_pid == NO_LAUNCHPAD_PID)
2668                         {
2669                                 pid = start_process(appid, app_path, kb);
2670                         }
2671                         else
2672                         {
2673                                 pid = app_send_cmd(pad_pid, cmd, kb);
2674                                 if (pid == AUL_R_ENOLAUNCHPAD && pad_pid == PROCESS_POOL_LAUNCHPAD_PID) {
2675                                         _W("Launch with legacy way");
2676                                         pad_pid = NO_LAUNCHPAD_PID;
2677                                         pid = start_process(appid, app_path, kb);
2678                                 }
2679                         }
2680                         if(pid == AUL_R_ECOMM) {
2681                                 pid = -ENOLAUNCHPAD;
2682                         }
2683                         aul_send_app_launch_request_signal(pid, appid, pkgid, component_type);
2684                         snprintf(trm_buf, MAX_PACKAGE_STR_SIZE, "appinfo_launch:%s[PID]%d", appid, pid);
2685                         __trm_app_info_send_socket(trm_buf);
2686                 }
2687                 if(pid < 0) {
2688 #ifdef _APPFW_FEATURE_AMD_KEY
2689                         if(_input_window_get() != 0)
2690                                 ecore_x_pointer_ungrab();
2691                         _D("pid(%d) ecore_x_pointer_ungrab", pid);
2692                         if(_key_ungrab(KEY_BACK) < 0) {
2693                                 _W("back key ungrab error");
2694                         } else {
2695                                 _D("back key ungrab");
2696                         }
2697 #endif
2698                 } else {
2699                         if (new_process) {
2700                                 _D("add app group info");
2701                                 app_group_start_app(pid, kb, lpid, can_attach, launch_mode);
2702
2703                                 if (component_type && strncmp(component_type, APP_TYPE_UI, strlen(APP_TYPE_UI)) == 0)
2704                                         __add_fgmgr_list(pid);
2705                         } else if (cmd == APP_START
2706                                         || cmd == APP_START_RES
2707                                         || cmd == APP_START_ASYNC
2708 #ifdef _APPFW_FEATURE_MULTI_INSTANCE
2709                                         || cmd == APP_START_MULTI_INSTANCE
2710 #endif
2711                                  ) {
2712                                 app_group_restart_app(pid, kb);
2713                         }
2714
2715 #ifdef _APPFW_FEATURE_AMD_KEY
2716                         grab_timer_id = g_timeout_add(1000, __grab_timeout_handler, (void *)pid);
2717 #endif
2718                 }
2719         }
2720         else if (component_type && strncmp(component_type, APP_TYPE_SERVICE, sizeof(APP_TYPE_SERVICE)) == 0) {
2721 #ifdef _APPFW_FEATURE_PRIVATE_SERVICE
2722                 const char *caller_appid = _status_app_get_appid_bypid(caller_pid);
2723
2724                 if (caller_appid) {
2725                         const struct appinfo *ai = NULL;
2726                         const char *caller_pkgid = NULL;
2727                         const char *pkgid = NULL;
2728
2729                         ai = appinfo_find(_laf, appid);
2730                         pkgid = appinfo_get_value(ai, AIT_PKGID);
2731                         ai = appinfo_find(_laf, caller_appid);
2732                         caller_pkgid = appinfo_get_value(ai, AIT_PKGID);
2733
2734                         if (caller_pkgid && pkgid && strcmp(caller_pkgid, pkgid) != 0) {
2735                                 const char *launch_type = bundle_get_val(kb, OSP_K_LAUNCH_TYPE);
2736
2737                                 if (launch_type == NULL || strcmp(launch_type, OSP_V_LAUNCH_TYPE_DATACONTROL) != 0) {
2738                                         const char *v = appinfo_get_value(ai, AIT_VISIBILITY);
2739                                         char num[256] = { 0, };
2740
2741                                         if (v == NULL) {
2742                                                 int vi_num = 0;
2743
2744                                                 if (__get_visibility_from_cert_svc(caller_pkgid, &vi_num) == 0) {
2745                                                         snprintf(num, 255, "%d", vi_num);
2746                                                         appinfo_set_value((struct appinfo*)ai, AIT_VISIBILITY, num);
2747                                                         v = num;
2748                                                 } else
2749                                                         _E("Failed to get visibility");
2750                                         }
2751
2752                                         if (v) {
2753                                                 int visibility = atoi(v);
2754                                                 if (!(visibility & CERT_SVC_VISIBILITY_PLATFORM)) {
2755                                                         _E("Couldn't launch service app in other packages");
2756                                                         __real_send(fd, -EREJECTED);
2757                                                         traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
2758                                                         return -EREJECTED;
2759                                                 }
2760                                         }
2761                                 }
2762                         }
2763                 }
2764 #endif
2765                 pid = _status_app_is_running_v2(appid);
2766
2767                 prelaunch_attribute = __get_prelaunch_attribute(ai);
2768
2769                 // 2.4 bg-categorized svc app || 2.3 svc app -> bg allowed
2770                 if ((prelaunch_attribute & RESOURCED_ALLOWED_BG_ATTRIBUTE)
2771                                 || !(prelaunch_attribute & RESOURCED_API_VER_2_4_ATTRIBUTE)) {
2772                         _D("[__SUSPEND__] allowed backgroudn, appid: %s", appid);
2773                         bundle_add(kb, AUL_K_ALLOWED_BG, "ALLOWED_BG");
2774                         bg_allowed = true;
2775                 }
2776
2777                 if (__grant_temporary_permission(caller_pid, appid, kb,
2778                         &caller_exec_label, &callee_exec_label, &paths) == 0)
2779                         grant_permission = 1;
2780
2781                 if (pid > 0) {
2782 #ifdef _APPFW_FEATURE_AMD_MODULE_LOG
2783                         strncpy(log_status, "RESUMING", strlen("RESUMING"));
2784 #endif
2785                         aul_send_app_resume_request_signal(pid, appid, pkgid, APP_TYPE_SERVICE);
2786
2787                         if (!bg_allowed)
2788                                 __prepare_to_wake_services(pid);
2789
2790                         if ((ret = __nofork_processing(cmd, pid, kb, fd)) < 0) {
2791                                 pid = ret;
2792                         } else {
2793                                 delay_reply = 1;
2794                         }
2795                 } else if (cmd != APP_RESUME) {
2796 #ifdef _APPFW_FEATURE_AMD_MODULE_LOG
2797                         strncpy(log_status, "LAUNCHING", strlen("LAUNCHING"));
2798 #endif
2799
2800                         __send_proc_prelaunch_signal(appid, pkgid, prelaunch_attribute);
2801
2802 #ifdef _APPFW_FEATURE_DEBUG_LAUNCHPAD
2803                         if (bundle_get_type(kb, AUL_K_SDK) != BUNDLE_TYPE_NONE) {
2804                                 SECURE_LOGD("The svcapp(%s) is launched by debug-launchpad", appid);
2805                                 __set_appinfo_for_launchpad(ai, kb);
2806                                 pid = app_send_cmd(DEBUG_LAUNCHPAD_PID, cmd, kb);
2807                                 if (pid == AUL_R_ECOMM) {
2808                                         pid = -ENOLAUNCHPAD;
2809                                 }
2810                         } else {
2811                                 pid = start_process(appid, app_path, kb);
2812                         }
2813 #else
2814                         pid = start_process(appid, app_path, kb);
2815 #endif
2816                         aul_send_app_launch_request_signal(pid, appid, pkgid, component_type);
2817                 }
2818
2819                 if (bg_allowed) {
2820                         g_idle_add(__check_service_only, (gpointer)pid);
2821                 }
2822
2823         } else {
2824                 _E("unkown application");
2825         }
2826
2827         if(!delay_reply) {
2828                 if(cmd == APP_START_ASYNC)
2829                         close(fd);
2830                 else
2831                         __real_send(fd, pid);
2832         }
2833
2834 #ifdef _APPFW_FEATURE_AMD_MODULE_LOG
2835         _status_log_save(log_status, appid);
2836 #endif
2837
2838         if(pid > 0) {
2839                 _status_add_app_info_list(appid, app_path, caller, pid, pad_pid, is_group_app);
2840                 _status_update_app_info_caller_pid(pid, caller_pid);
2841                 if (grant_permission) {
2842                         __add_shared_info(pid, caller_exec_label, callee_exec_label, paths);
2843                         if (caller_exec_label) {
2844                                 free(caller_exec_label);
2845                                 caller_exec_label = NULL;
2846                         }
2847                         if (callee_exec_label) {
2848                                 free(callee_exec_label);
2849                                 callee_exec_label = NULL;
2850                         }
2851                 }
2852
2853 #ifdef _APPFW_FEATURE_AMD_MODULE_LOG
2854                 snprintf(log_status, AUL_PR_NAME, "SUCCESS: %d", pid);
2855                 _status_log_save(log_status, appid);
2856 #endif
2857
2858 #ifdef _APPFW_FEATURE_APP_CHECKER
2859                 pkg_type = appinfo_get_value(ai, AIT_TYPE);
2860                 if (!pkg_type) {
2861                         traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
2862                         return -1;
2863                 }
2864
2865                 ret = ac_server_check_launch_privilege(appid, pkg_type, pid);
2866                 if (ret != AC_R_ERROR) {
2867                         traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
2868                         return pid;
2869                 } else {
2870                         traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
2871                         return -1;
2872                 }
2873 #endif
2874         }
2875 #ifdef _APPFW_FEATURE_AMD_MODULE_LOG
2876         else {
2877                 _status_log_save("FAILURE", appid);
2878         }
2879 #endif
2880
2881         traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
2882         return pid;
2883 }
2884
2885 int __e17_status_handler(int pid, int status, void *data)
2886 {
2887         if (status == PROC_STATUS_FG) {
2888                 __del_fgmgr_list(pid);
2889
2890 #ifdef _APPFW_FEATURE_AMD_KEY
2891                 _D("pid(%d) status(%d)", pid, status);
2892                 if(_input_window_get() != 0)
2893                         ecore_x_pointer_ungrab();
2894                 if(_key_ungrab(KEY_BACK) < 0) {
2895                         _W("back key ungrab error");
2896                 } else {
2897                         _D("back key ungrab");
2898                 }
2899                 g_source_remove(grab_timer_id);
2900 #endif
2901                 _status_update_app_info_list(pid, STATUS_VISIBLE, FALSE);
2902 #ifdef _APPFW_FEATURE_TERMINATE_UNMANAGEABLE_APP
2903 #ifdef _APPFW_FEATURE_SEND_HOME_LAUNCH_SIGNAL
2904                 __check_home_app(pid);
2905 #endif
2906 #endif
2907         } else if (status == PROC_STATUS_BG) {
2908                 _status_update_app_info_list(pid, STATUS_BG, FALSE);
2909         } else if (status == PROC_STATUS_LAUNCH) {
2910                 _D("pid(%d) status(%d)", pid, status);
2911                 traceBegin(TTRACE_TAG_APPLICATION_MANAGER, "AUL:AMD:EFFECT_DONE");
2912                 traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
2913         }
2914
2915         return 0;
2916 }
2917
2918 int _launch_init(struct amdmgr* amd)
2919 {
2920         int ret = 0;
2921
2922         _D("_launch_init");
2923
2924         amd_cmdline = __proc_get_cmdline_bypid(getpid());
2925
2926         _laf = amd->af;
2927
2928         conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
2929         if (!conn) {
2930                 _E("dbus_bus_get error");
2931                 return -EBADMSG;
2932         }
2933
2934         ret = aul_listen_e17_status_signal(__e17_status_handler, NULL);
2935
2936         _D("ret : %d", ret);
2937
2938         __preexec_init(0, NULL);
2939
2940         return 0;
2941 }
2942
2943 void _set_atom_effect(void)
2944 {
2945         ATOM_IMAGE_EFFECT = ecore_x_atom_get(atom_name);
2946 }
2947
2948 void __set_appinfo_for_launchpad(const struct appinfo *ai, bundle *kb) {
2949         int ret;
2950
2951         _D("Add hwacc, taskmanage, app_path and pkg_type into bundle for sending those to launchpad.");
2952         ret = bundle_del(kb, AUL_K_HWACC);
2953         if (ret != BUNDLE_ERROR_NONE)
2954                 _D("bundle_del error: %d", ret);
2955         ret = bundle_add(kb, AUL_K_HWACC, appinfo_get_value(ai, AIT_HWACC));
2956         if (ret != BUNDLE_ERROR_NONE)
2957                 _E("failed to set AUL_K_HWACC: ret(%d)", ret);
2958
2959
2960         bundle_del(kb, AUL_K_TASKMANAGE);
2961         if (ret != BUNDLE_ERROR_NONE)
2962                 _D("bundle_del error: %d", ret);
2963         bundle_add(kb, AUL_K_TASKMANAGE, appinfo_get_value(ai, AIT_TASKMANAGE));
2964         if (ret != BUNDLE_ERROR_NONE)
2965                 _E("failed to set AUL_K_TASKMANAGE: ret(%d)", ret);
2966
2967         bundle_del(kb, AUL_K_EXEC);
2968         if (ret != BUNDLE_ERROR_NONE)
2969                 _D("bundle_del error: %d", ret);
2970         bundle_add(kb, AUL_K_EXEC, appinfo_get_value(ai, AIT_EXEC));
2971         if (ret != BUNDLE_ERROR_NONE)
2972                 _E("failed to set AUL_K_EXEC: ret(%d)", ret);
2973
2974         bundle_del(kb, AUL_K_PACKAGETYPE);
2975         if (ret != BUNDLE_ERROR_NONE)
2976                 _D("bundle_del error: %d", ret);
2977         bundle_add(kb, AUL_K_PACKAGETYPE, appinfo_get_value(ai, AIT_TYPE));
2978         if (ret != BUNDLE_ERROR_NONE)
2979                 _E("failed to set AUL_K_PACKAGETYPE: ret(%d)", ret);
2980
2981         bundle_del(kb, AUL_K_INTERNAL_POOL);
2982         if (ret != BUNDLE_ERROR_NONE)
2983                 _D("bundle_del error: %d", ret);
2984         bundle_add(kb, AUL_K_INTERNAL_POOL, appinfo_get_value(ai, AIT_POOL));
2985         if (ret != BUNDLE_ERROR_NONE)
2986                 _E("failed to set AUL_K_INTERNAL_POOL: ret(%d)", ret);
2987
2988         bundle_del(kb, AUL_K_PKGID);
2989         if (ret != BUNDLE_ERROR_NONE)
2990                 _D("bundle_del error: %d", ret);
2991         bundle_add(kb, AUL_K_PKGID, appinfo_get_value(ai, AIT_PKGID));
2992         if (ret != BUNDLE_ERROR_NONE)
2993                 _E("failed to set AUL_K_PKGID: ret(%d)", ret);
2994 }