Tizen 2.4.0 rev3 SDK Public Release
[framework/appfw/aul-1.git] / am_daemon / amd_request.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 <sys/types.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #include <dirent.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <dlfcn.h>
31 #include <poll.h>
32 #include <aul.h>
33 #include <glib.h>
34 #include <bundle.h>
35 #include <bundle_internal.h>
36 #include <rua.h>
37 #include <rua_stat.h>
38 #include <proc_stat.h>
39 #include <security-server.h>
40 #include <vconf.h>
41 #include <ttrace.h>
42 #include <Ecore.h>
43 #include <sys/signalfd.h>
44 #include <signal.h>
45 #include <wait.h>
46 #include <pkgmgr-info.h>
47
48 #include "amd_config.h"
49 #include "simple_util.h"
50 #include "app_sock.h"
51 #include "app_signal.h"
52 #include "aul_util.h"
53 #include "amd_request.h"
54 #include "amd_key.h"
55 #include "amd_launch.h"
56 #include "amd_appinfo.h"
57 #include "amd_status.h"
58 #include "amd_app_group.h"
59
60 #define INHOUSE_UID     5000
61
62 #ifdef _APPFW_FEATURE_MULTI_INSTANCE
63 #define METADATA_MULTI_INSTANCE         "http://developer.samsung.com/tizen/metadata/multiinstance"
64 #endif
65
66 struct appinfomgr *_raf;
67 static DBusConnection *bus = NULL;
68 static sigset_t oldmask;
69 #ifdef _APPFW_FEATURE_SEND_HOME_LAUNCH_SIGNAL
70 char *home_appid = NULL;
71 #endif
72
73 struct restart_info {
74         char *appid;
75         int count;
76         Ecore_Timer *timer;
77 };
78
79 GHashTable *restart_tbl;
80
81 static int __send_result_to_client(int fd, int res);
82 static gboolean __request_handler(gpointer data);
83
84 // TODO: Replace with pkgmgr-info header
85 int pkgmgrinfo_updateinfo_check_update(const char* pkgid);
86
87 static int __send_result_data(int fd, int cmd, unsigned char *kb_data, int datalen)
88 {
89         int len;
90         int ret;
91         int res = 0;
92         app_pkt_t *pkt = NULL;
93
94         if (datalen > AUL_SOCK_MAXBUFF - 8) {
95                 _E("datalen > AUL_SOCK_MAXBUFF\n");
96                 return -EINVAL;
97         }
98
99         pkt = (app_pkt_t *) malloc(sizeof(char) * AUL_SOCK_MAXBUFF);
100         if (NULL == pkt) {
101                 _E("Malloc Failed!");
102                 return -ENOMEM;
103         }
104         memset(pkt, 0, AUL_SOCK_MAXBUFF);
105
106         pkt->cmd = cmd;
107         pkt->len = datalen;
108         memcpy(pkt->data, kb_data, datalen);
109
110         if ((len = send(fd, pkt, datalen + 8, MSG_NOSIGNAL)) != datalen + 8) {
111                 _E("sendto() failed - %d %d (errno %d)", len, datalen + 8, errno);
112                 if(len > 0) {
113                         while (len != datalen + 8) {
114                                 ret = send(fd, &pkt->data[len-8], datalen + 8 - len, MSG_NOSIGNAL);
115                                 if (ret < 0) {
116                                         _E("second sendto() failed - %d %d (errno %d)", ret, datalen + 8, errno);
117                                         close(fd);
118                                         if (pkt) {
119                                                 free(pkt);
120                                                 pkt = NULL;
121                                         }
122                                         return -ECOMM;
123                                 }
124                                 len += ret;
125                                 _D("sendto() len - %d %d", len, datalen + 8);
126                         }
127                 } else {
128                         close(fd);
129                         if (pkt) {
130                                 free(pkt);
131                                 pkt = NULL;
132                         }
133                         return -ECOMM;
134                 }
135         }
136         if (pkt) {
137                 free(pkt);
138                 pkt = NULL;
139         }
140
141         close(fd);
142         return res;
143 }
144
145 static int __send_result_to_client(int fd, int res)
146 {
147         _W("__send_result_to_client, pid: %d", res);
148
149         if (send(fd, &res, sizeof(int), MSG_NOSIGNAL) < 0) {
150                 if (errno == EPIPE) {
151                         _E("send failed due to EPIPE.\n");
152                 }
153
154                 _E("send fail to client");
155         }
156         close(fd);
157         return 0;
158 }
159
160 static void __real_send(int clifd, int ret)
161 {
162         if(clifd <= 0) {
163                 return;
164         }
165         if (send(clifd, &ret, sizeof(int), MSG_NOSIGNAL) < 0) {
166                 if (errno == EPIPE) {
167                         _E("send failed due to EPIPE.\n");
168                 }
169                 _E("send fail to client");
170         }
171
172         close(clifd);
173 }
174
175 static int __get_caller_pid(bundle *kb)
176 {
177         const char *pid_str;
178         int pid;
179
180         pid_str = bundle_get_val(kb, AUL_K_ORG_CALLER_PID);
181         if(pid_str)
182                 goto end;
183
184         pid_str = bundle_get_val(kb, AUL_K_CALLER_PID);
185         if (pid_str == NULL)
186                 return -1;
187
188 end:
189         pid = atoi(pid_str);
190         if (pid <= 1)
191                 return -1;
192
193         return pid;
194 }
195
196 static int __foward_cmd(int cmd, bundle *kb, int cr_pid)
197 {
198         int pid;
199         int pgid;
200         char tmp_pid[MAX_PID_STR_BUFSZ];
201         int datalen;
202         bundle_raw *kb_data;
203         int res;
204
205         if ((pid = __get_caller_pid(kb)) < 0)
206         {
207                 return AUL_R_ERROR;
208         }
209
210         pgid = getpgid(cr_pid);
211         if(pgid > 0) {
212                 snprintf(tmp_pid, MAX_PID_STR_BUFSZ, "%d", pgid);
213                 bundle_del(kb, AUL_K_CALLEE_PID);
214                 bundle_add(kb, AUL_K_CALLEE_PID, tmp_pid);
215         }
216
217         _W("__forward_cmd: %d %d", cr_pid, pgid);
218
219         bundle_encode(kb, &kb_data, &datalen);
220         if ((res = __app_send_raw_with_noreply(pid, cmd, kb_data, datalen)) < 0)
221                 res = AUL_R_ERROR;
222
223         free(kb_data);
224
225         return res;
226 }
227
228 static int __app_process_by_pid(int cmd,
229         const char *pkg_name, struct ucred *cr, int clifd)
230 {
231         int pid;
232         int ret = -1;
233         int dummy;
234         char *appid = NULL;
235         const char *pkgid = NULL;
236         const char *type = NULL;
237         const struct appinfo *ai = NULL;
238
239         if (pkg_name == NULL)
240                 return -1;
241
242         pid = atoi(pkg_name);
243         if (pid <= 1) {
244                 _E("invalid pid");
245                 return -1;
246         }
247
248         /* check whether app process is dead or not */
249         char buf[1024];
250         snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid);
251         if (access(buf, F_OK) != 0) {
252                 _E("pid(%d) is dead. cmd(%d) is canceled", pid, cmd);
253                 __real_send(clifd, -ECOMM);
254                 return -ECOMM;
255         }
256
257         if (_status_get_app_info_status(pid) == -1) {
258                 char buf[512];
259                 if (_status_get_pkgname_bypid(pid, buf, 512) == -1) {
260                         _E("request for unknown pid. It might not be a pid of app: %d", pid);
261                         return -1;
262                 }
263         }
264
265         appid = _status_app_get_appid_bypid(pid);
266         ai = appinfo_find(_raf, appid);
267         pkgid = appinfo_get_value(ai, AIT_PKGID);
268         type = appinfo_get_value(ai, AIT_COMPTYPE);
269
270         if (cmd == APP_RESUME_BY_PID)
271                 aul_send_app_resume_request_signal(pid, appid, pkgid, type);
272         else
273                 aul_send_app_terminate_request_signal(pid, appid, pkgid, type);
274
275         SECURE_LOGD("__app_process_by_pid, pid: %d, ", pid);
276         switch (cmd) {
277         case APP_RESUME_BY_PID:
278                 ret = _resume_app(pid, clifd);
279                 break;
280         case APP_TERM_BY_PID:
281         case APP_TERM_BY_PID_WITHOUT_RESTART:
282                 ret = _term_app(pid, clifd);
283                 break;
284         case APP_TERM_BGAPP_BY_PID:
285                 ret = _term_bgapp(pid, clifd);
286                 break;
287         case APP_KILL_BY_PID:
288                 if ((ret = _send_to_sigkill(pid)) < 0)
289                         _E("fail to killing - %d\n", pid);
290                 __real_send(clifd, ret);
291                 break;
292         case APP_TERM_REQ_BY_PID:
293                 ret = _term_req_app(pid, clifd);
294                 break;
295         case APP_TERM_BY_PID_ASYNC:
296                 if ((ret = __app_send_raw_with_noreply(pid, cmd, (unsigned char *)&dummy, sizeof(int))) < 0) {
297                         _D("terminate req packet send error");
298                 }
299                 __real_send(clifd, ret);
300                 break;
301         case APP_PAUSE_BY_PID:
302                 ret = _pause_app(pid, clifd);
303                 break;
304         default:
305                 break;
306         }
307
308         return ret;
309 }
310
311 #ifdef _APPFW_FEATURE_EFFECTIVE_APPID
312 static void __set_effective_appid(bundle *kb)
313 {
314         const struct appinfo *ai;
315         const struct appinfo *effective_ai;
316         char *appid;
317         const char *effective_appid;
318
319         appid = (char *)bundle_get_val(kb, AUL_K_PKG_NAME);
320         if (appid) {
321                 ai = appinfo_find(_raf, appid);
322                 if (ai == NULL)
323                         return;
324
325                 effective_appid = appinfo_get_value(ai, AIT_EFFECTIVE_APPID);
326                 if (effective_appid) {
327                         const char *pkgid;
328                         const char *effective_pkgid;
329                         effective_ai = appinfo_find(_raf, effective_appid);
330                         if (effective_ai == NULL)
331                                 return;
332
333                         pkgid = appinfo_get_value(ai, AIT_PKGID);
334                         effective_pkgid = appinfo_get_value(effective_ai, AIT_PKGID);
335                         if (pkgid && effective_pkgid && strcmp(pkgid, effective_pkgid) == 0) {
336                                 _D("use effective appid instead of the real appid");
337                                 bundle_del(kb, AUL_K_PKG_NAME);
338                                 bundle_add(kb, AUL_K_PKG_NAME, effective_appid);
339                         }
340                 }
341         }
342 }
343 #endif
344
345 static gboolean __add_history_handler(gpointer user_data)
346 {
347         struct rua_rec rec;
348         int ret;
349         bundle *kb = NULL;
350         char *appid = NULL;
351         char *app_path = NULL;
352         char *stat_caller = NULL;
353         char *stat_tag = NULL;
354         struct appinfo *ai;
355
356         app_pkt_t *pkt = (app_pkt_t *)user_data;
357
358         if (!pkt)
359                 return FALSE;
360         kb = bundle_decode(pkt->data, pkt->len);
361
362         if (!app_group_is_group_app(kb)) {
363
364 #ifdef _APPFW_FEATURE_EFFECTIVE_APPID
365                 __set_effective_appid(kb);
366 #endif
367                 appid = (char *)bundle_get_val(kb, AUL_K_PKG_NAME);
368
369                 ai = (struct appinfo *)appinfo_find(_raf, appid);
370                 app_path = (char *)appinfo_get_value(ai, AIT_EXEC);
371
372                 memset((void *)&rec, 0, sizeof(rec));
373
374                 rec.pkg_name = appid;
375                 rec.app_path = app_path;
376
377                 if(pkt->len > 0) {
378                         rec.arg = (char *)pkt->data;
379                 }
380
381                 SECURE_LOGD("add rua history %s %s", rec.pkg_name, rec.app_path);
382
383                 ret = rua_add_history(&rec);
384                 if (ret == -1)
385                         _D("rua add history error");
386         }
387
388         stat_caller = (char *)bundle_get_val(kb, AUL_SVC_K_RUA_STAT_CALLER);
389         stat_tag = (char *)bundle_get_val(kb, AUL_SVC_K_RUA_STAT_TAG);
390
391         if (stat_caller != NULL && stat_tag != NULL) {
392                 SECURE_LOGD("rua_stat_caller: %s, rua_stat_tag: %s", stat_caller, stat_tag);
393                 rua_stat_update(stat_caller, stat_tag);
394         }
395
396         if (kb != NULL)
397                 bundle_free(kb);
398         free(pkt);
399
400         return FALSE;
401 }
402
403 static int __get_pid_cb(void *user_data, const char *group, pid_t pid)
404 {
405         int *sz = user_data;
406
407         _D("%s: %d : %d", *sz, pid);
408         *sz = 1; /* 1 is enough */
409
410         return -1; /* stop the iteration */
411 }
412
413 int _release_srv(const char *appid)
414 {
415         int r;
416         const struct appinfo *ai;
417
418         ai = (struct appinfo *)appinfo_find(_raf, appid);
419         if (!ai) {
420                 _E("appid not found");
421                 SECURE_LOGE("release service: '%s' not found", appid);
422                 return -1;
423         }
424
425         r = appinfo_get_boolean(ai, AIT_RESTART);
426         if (r == 1) {
427                 _W("Auto restart");
428                 SECURE_LOGD("Auto restart set: '%s'", appid);
429                 return _start_srv(ai);
430         }
431
432         return 0;
433 }
434
435 static Eina_Bool __restart_timeout_handler(void *data)
436 {
437         struct restart_info *ri = (struct restart_info *)data;
438
439         _D("ri (%x)", ri);
440         SECURE_LOGD("appid (%s)", ri->appid);
441
442         g_hash_table_remove(restart_tbl, ri->appid);
443         free(ri->appid);
444         free(ri);
445
446         return ECORE_CALLBACK_CANCEL;
447 }
448
449 static bool __check_restart(const char *appid)
450 {
451         struct restart_info *ri = NULL;
452         //struct appinfo *ai = NULL;
453
454         ri = g_hash_table_lookup(restart_tbl, appid);
455
456         if(!ri) {
457                 ri = calloc(1, sizeof(*ri));
458                 if (!ri) {
459                         _E("create restart info: %s", strerror(errno));
460                         return true;
461                 }
462                 memset(ri, 0, sizeof(struct restart_info));
463                 ri->appid = strdup(appid);
464                 ri->count = 1;
465                 g_hash_table_insert(restart_tbl, ri->appid, ri);
466
467                 _D("ri (%x)", ri);
468                 SECURE_LOGD("appid (%s)", appid);
469
470                 ri->timer = ecore_timer_add(10, __restart_timeout_handler, ri);
471         } else {
472                 ri->count++;
473                 _D("count (%d)", ri->count);
474                 if(ri->count > 5) {
475                         /*ai = appinfo_find(_raf, appid);
476                         if(ai) {
477                                 appinfo_set_value(ai, AIT_STATUS, "norestart");
478                         }*/
479                         ecore_timer_del(ri->timer);
480                         return false;
481                 }
482         }
483         return true;
484 }
485
486 #ifdef _APPFW_FEATURE_SEND_HOME_LAUNCH_SIGNAL
487 static inline int __send_home_launch_signal(int pid)
488 {
489         DBusMessage *message;
490
491         if (bus == NULL)
492                 return -1;
493
494         message = dbus_message_new_signal(AUL_DBUS_PATH,
495                                           AUL_DBUS_SIGNAL_INTERFACE,
496                                           AUL_DBUS_HOMELAUNCH_SIGNAL);
497
498         if (dbus_message_append_args(message,
499                                      DBUS_TYPE_UINT32, &pid,
500                                      DBUS_TYPE_INVALID) == FALSE) {
501                 _E("Failed to load data error");
502                 return -1;
503         }
504
505         if (dbus_connection_send(bus, message, NULL) == FALSE) {
506                 _E("dbus send error");
507                 return -1;
508         }
509
510         dbus_connection_flush(bus);
511         dbus_message_unref(message);
512
513         _W("send a home launch signal");
514
515         return 0;
516 }
517 #endif
518
519 static inline int __send_app_termination_signal(int dead_pid)
520 {
521         DBusMessage *message;
522
523         if (bus == NULL)
524                 return -1;
525
526         message = dbus_message_new_signal(AUL_DBUS_PATH,
527                                           AUL_DBUS_SIGNAL_INTERFACE,
528                                           AUL_DBUS_APPDEAD_SIGNAL);
529
530         if (dbus_message_append_args(message,
531                                      DBUS_TYPE_UINT32, &dead_pid,
532                                      DBUS_TYPE_INVALID) == FALSE) {
533                 _E("Failed to load data error");
534                 return -1;
535         }
536
537         if (dbus_connection_send(bus, message, NULL) == FALSE) {
538                 _E("dbus send error");
539                 return -1;
540         }
541
542         dbus_connection_flush(bus);
543         dbus_message_unref(message);
544
545         _W("send dead signal done");
546
547         return 0;
548 }
549
550 int _send_set_process_group_signal_signal(int owner_pid, int child_pid)
551 {
552         DBusMessage *message;
553
554         if (bus == NULL)
555                 return -1;
556
557         message = dbus_message_new_signal(RESOURCED_PROC_OBJECT,
558                                           RESOURCED_PROC_INTERFACE,
559                                           RESOURCED_PROC_GROUP_SIGNAL);
560
561         if (dbus_message_append_args(message,
562                                          DBUS_TYPE_INT32, &owner_pid,
563                                          DBUS_TYPE_INT32, &child_pid,
564                                          DBUS_TYPE_INVALID) == FALSE) {
565                 _E("Failed to load data error");
566                 return -1;
567         }
568
569         if (dbus_connection_send(bus, message, NULL) == FALSE) {
570                 _E("dbus send error");
571                 return -1;
572         }
573
574         dbus_connection_flush(bus);
575         dbus_message_unref(message);
576
577         _W("send set_process_group signal done");
578
579         return 0;
580 }
581
582 #ifdef _APPFW_FEATURE_MULTI_INSTANCE
583 static char* __get_metadata_value(const char *appid, const char *metadata_key)
584 {
585         int ret = 0;
586         pkgmgrinfo_appinfo_h handle;
587         char *metadata_value = NULL;
588         char *multi_appid = NULL;
589
590         ret = pkgmgrinfo_appinfo_get_appinfo(appid, &handle);
591         if (ret != PMINFO_R_OK)
592                 return NULL;
593
594         ret = pkgmgrinfo_appinfo_get_metadata_value(handle, metadata_key, &metadata_value);
595         if (ret != PMINFO_R_OK) {
596                 pkgmgrinfo_appinfo_destroy_appinfo(handle);
597                 return NULL;
598         }
599
600         multi_appid = strdup(metadata_value);
601
602         pkgmgrinfo_appinfo_destroy_appinfo(handle);
603
604         return multi_appid;
605 }
606
607 static const char* __check_target_appid(const struct appinfo* ai, const char *appid, const char *multi_appid)
608 {
609         const char* target = NULL;
610
611         // Both apps are running
612         if (_status_app_is_running(appid) != -1 && _status_app_is_running(multi_appid) != -1) {
613                 const char* toggle = appinfo_get_value(ai, AIT_TOGGLE_ORDER);
614                 int order = atoi(toggle);
615
616                 _D("launch a multi-instance app with toggle mode: %d", order);
617                 switch (order) {
618                         case 0:
619                                 target = multi_appid;
620                                 appinfo_set_value((struct appinfo *)ai,
621                                         AIT_TOGGLE_ORDER, "1");
622                                 break;
623
624                         case 1:
625                                 target = appid;
626                                 appinfo_set_value((struct appinfo *)ai,
627                                         AIT_TOGGLE_ORDER, "0");
628                                 break;
629
630                         default:
631                                 break;
632                 }
633         } else {
634                 // Main app is running
635                 if (_status_app_is_running(appid) != -1) {
636                         SECURE_LOGD("Send a request to the running main appid: %s", appid);
637                         target = appid;
638                         // Sub app is running
639                 } else if (_status_app_is_running(multi_appid) != -1) {
640                         SECURE_LOGD("Send a request to the running sub appid: %s", multi_appid);
641                         target = multi_appid;
642                 } else {
643                         SECURE_LOGD("Both apps are not running, launch a main app - %s", appid);
644                         target = appid;
645                 }
646         }
647
648         return target;
649 }
650 #endif
651
652 static void __dispatch_app_group_get_window(int clifd, const app_pkt_t *pkt)
653 {
654         bundle *b;
655         char *buf;
656         int pid;
657         int wid;
658
659         b = bundle_decode(pkt->data, pkt->len);
660         bundle_get_str(b, AUL_K_PID, &buf);
661         pid = atoi(buf);
662         bundle_free(b);
663         wid = app_group_get_window(pid);
664         __real_send(clifd, wid);
665 }
666
667 static void __dispatch_app_group_set_window(int clifd, const app_pkt_t *pkt, int pid)
668 {
669         bundle *b;
670         char *buf;
671         int wid;
672         int ret;
673
674         b = bundle_decode(pkt->data, pkt->len);
675         bundle_get_str(b, AUL_K_WID, &buf);
676         wid = atoi(buf);
677         bundle_free(b);
678         ret = app_group_set_window(pid, wid);
679         __real_send(clifd, ret);
680 }
681
682 static void __dispatch_app_group_get_fg_flag(int clifd, const app_pkt_t *pkt)
683 {
684         bundle *b;
685         char *buf;
686         int pid;
687         int fg;
688
689         b = bundle_decode(pkt->data, pkt->len);
690         bundle_get_str(b, AUL_K_PID, &buf);
691         pid = atoi(buf);
692         bundle_free(b);
693         fg = app_group_get_fg_flag(pid);
694         __real_send(clifd, fg);
695 }
696
697 static void __dispatch_app_group_clear_top(int clifd, int pid)
698 {
699         app_group_clear_top(pid);
700         __real_send(clifd, 0);
701 }
702
703 static void __dispatch_app_group_get_leader_pid(int clifd,
704                 const app_pkt_t *pkt)
705 {
706         bundle *b;
707         char *buf;
708         int pid;
709         int lpid;
710
711         b = bundle_decode(pkt->data, pkt->len);
712         bundle_get_str(b, AUL_K_PID, &buf);
713         pid = atoi(buf);
714         bundle_free(b);
715         lpid = app_group_get_leader_pid(pid);
716         __real_send(clifd, lpid);
717 }
718
719 static void __dispatch_app_group_get_leader_pids(int clifd,
720                 const app_pkt_t *pkt)
721 {
722         int cnt;
723         int *pids;
724         unsigned char empty[1] = { 0 };
725
726         app_group_get_leader_pids(&cnt, &pids);
727
728         if (pids == NULL || cnt == 0) {
729                 __send_result_data(clifd, APP_GROUP_GET_LEADER_PIDS, empty, 0);
730         } else {
731                 __send_result_data(clifd, APP_GROUP_GET_LEADER_PIDS,
732                         (unsigned char *)pids, cnt * sizeof(int));
733         }
734         if (pids != NULL)
735                 free(pids);
736 }
737
738 static void __dispatch_app_group_get_idle_pids(int clifd,
739                 const app_pkt_t *pkt)
740 {
741         int cnt;
742         int *pids;
743         unsigned char empty[1] = { 0 };
744
745         app_group_get_idle_pids(&cnt, &pids);
746
747         if (pids == NULL || cnt == 0) {
748                 __send_result_data(clifd, APP_GROUP_GET_IDLE_PIDS, empty, 0);
749         } else {
750                 __send_result_data(clifd, APP_GROUP_GET_IDLE_PIDS,
751                         (unsigned char *)pids, cnt * sizeof(int));
752         }
753         if (pids != NULL)
754                 free(pids);
755 }
756
757 static void __dispatch_app_group_get_group_pids(int clifd, const app_pkt_t *pkt)
758 {
759         bundle *b;
760         char *buf;
761         int leader_pid;
762         int cnt;
763         int *pids;
764         unsigned char empty[1] = { 0 };
765
766         b = bundle_decode(pkt->data, pkt->len);
767         bundle_get_str(b, AUL_K_LEADER_PID, &buf);
768         leader_pid = atoi(buf);
769         bundle_free(b);
770
771         app_group_get_group_pids(leader_pid, &cnt, &pids);
772         if (pids == NULL || cnt == 0) {
773                 __send_result_data(clifd, APP_GROUP_GET_GROUP_PIDS, empty, 0);
774         } else {
775                 __send_result_data(clifd, APP_GROUP_GET_GROUP_PIDS,
776                         (unsigned char *)pids, cnt * sizeof(int));
777         }
778         if (pids != NULL)
779                 free(pids);
780 }
781
782 static void __dispatch_app_group_lower(int clifd, int pid)
783 {
784         int ret = 0;
785
786         app_group_lower(pid, &ret);
787         __real_send(clifd, ret);
788 }
789
790 static void  __check_host_pid(bundle *kb, struct ucred *cr)
791 {
792         if (cr->pid == 0) {
793                 SECURE_LOGD("check host pid");
794
795                 char *spid = NULL;
796
797                 bundle_get_str(kb, AUL_K_HOST_PID, &spid);
798                 if (spid != NULL) {
799                         cr->pid = atoi(spid);
800                         SECURE_LOGD("caller pid was changed by host pid %s", spid);
801                 }
802         }
803 }
804
805 static gboolean __request_handler(gpointer data)
806 {
807         GPollFD *gpollfd = (GPollFD *) data;
808         int fd = gpollfd->fd;
809         app_pkt_t *pkt;
810         int clifd;
811         struct ucred cr;
812         int *status;
813         int ret = -1;
814         int free_pkt = 1;
815         char *appid = NULL;
816         char *term_pid = NULL;
817         int pid;
818         bundle *kb = NULL;
819         const struct appinfo *ai;
820         int owner_pid;
821         int child_pid;
822
823         traceBegin(TTRACE_TAG_APPLICATION_MANAGER, "AUL:AMD:REQ_HANDLER");
824         if ((pkt = __app_recv_raw(fd, &clifd, &cr)) == NULL) {
825                 _E("recv error");
826                 traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
827                 return FALSE;
828         }
829
830         kb = bundle_decode(pkt->data, pkt->len);
831 #ifdef _APPFW_FEATURE_EFFECTIVE_APPID
832         __set_effective_appid(kb);
833 #endif
834         _D("__request_handler: %d", pkt->cmd);
835
836         switch (pkt->cmd) {
837                 case APP_OPEN:
838                 case APP_RESUME:
839                 case APP_START:
840                 case APP_START_RES:
841                 case APP_START_ASYNC:
842                         ret = security_server_check_privilege_by_sockfd(clifd, "aul::launch", "x");
843                         if (cr.pid != 0 && ret == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) {
844                                 _E("launch request has been denied by smack");
845                                 ret = -EILLEGALACCESS;
846                                 __real_send(clifd, ret);
847                         } else {
848                                 __check_host_pid(kb, &cr);
849                                 appid = (char *)bundle_get_val(kb, AUL_K_PKG_NAME);
850
851 #ifdef _APPFW_FEATURE_MULTI_INSTANCE
852                                 // Check the multi-instance app
853                                 ai = appinfo_find(_raf, appid);
854                                 if (ai == NULL) {
855                                         _E("no appinfo");
856                                         __real_send(clifd, -ENOAPP);
857                                 } else {
858                                         const char* multi = appinfo_get_value(ai, AIT_MULTI_INSTANCE);
859                                         if( multi && strncmp(multi, "true", strlen("true")) == 0 ) {
860
861                                                 char* multi_appid =__get_metadata_value(appid, METADATA_MULTI_INSTANCE);
862                                                 if (multi_appid != NULL)
863                                                 {
864                                                         SECURE_LOGD("Multi-instance main: %s, sub: %s", appid, multi_appid);
865                                                         const char* target_appid = __check_target_appid(ai, appid, multi_appid);
866
867                                                         SECURE_LOGD("launch a target appid: - %s", target_appid);
868                                                         ret = _start_app(target_appid, kb, pkt->cmd, cr.pid, cr.uid, clifd);
869                                                 } else {
870                                                         SECURE_LOGD("No multi-instance app information, launch a main appid: - %s", appid);
871                                                         ret = _start_app(appid, kb, pkt->cmd, cr.pid, cr.uid, clifd);
872                                                 }
873
874                                                 free(multi_appid);
875                                         }
876                                         else
877                                         {
878                                                 SECURE_LOGD("launch a single-instance appid: %s", appid);
879                                                 ret = _start_app(appid, kb, pkt->cmd, cr.pid, cr.uid, clifd);
880                                         }
881                                 }
882 #else
883                                 ret = _start_app(appid, kb, pkt->cmd, cr.pid, cr.uid, clifd);
884 #endif
885
886                                 if(ret > 0 && bundle_get_type(kb, AUL_K_PRELAUCHING) == BUNDLE_TYPE_NONE) {
887 #ifdef _APPFW_FEATURE_BG_PROCESS_LIMIT
888                                         if (!app_group_is_group_app(kb)) {
889                                                 item_pkt_t *item = g_malloc0(sizeof(item_pkt_t));
890                                                 item->pid = ret;
891                                                 strncpy(item->appid, appid, 511);
892                                                 __add_item_running_list(item);
893                                                 g_free(item);
894                                         }
895 #endif
896 #ifdef _APPFW_FEATURE_SEND_HOME_LAUNCH_SIGNAL
897                                         if (home_appid && strncmp(appid, home_appid, strlen(appid)) == 0)
898                                                 __send_home_launch_signal(ret);
899 #endif
900                                         g_timeout_add(1500, __add_history_handler, pkt);
901                                         free_pkt = 0;
902                                 }
903
904                                 if (kb != NULL)
905                                         bundle_free(kb), kb = NULL;
906                         }
907                         break;
908 #ifdef _APPFW_FEATURE_MULTI_INSTANCE
909                 case APP_START_MULTI_INSTANCE:
910                         ret = security_server_check_privilege_by_sockfd(clifd, "aul::launch", "x");
911                         if (cr.pid != 0 && ret == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) {
912                                 _E("launch request has been denied by smack");
913                                 ret = -EILLEGALACCESS;
914                                 __real_send(clifd, ret);
915                         } else {
916                                 appid = (char *)bundle_get_val(kb, AUL_K_PKG_NAME);
917
918                                 SECURE_LOGD("launch a multi-instance appid: %s", appid);
919                                 ret = _start_app(appid, kb, pkt->cmd, cr.pid, cr.uid, clifd);
920                         }
921
922                         if(ret > 0) {
923 #ifdef _APPFW_FEATURE_BG_PROCESS_LIMIT
924                                 if (!app_group_is_group_app(kb)) {
925                                         item_pkt_t *item = g_malloc0(sizeof(item_pkt_t));
926                                         item->pid = ret;
927                                         strncpy(item->appid, appid, 511);
928                                         __add_item_running_list(item);
929                                         g_free(item);
930                                 }
931 #endif
932 #ifdef _APPFW_FEATURE_SEND_HOME_LAUNCH_SIGNAL
933                                 if (home_appid && strncmp(appid, home_appid, strlen(appid)) == 0)
934                                         __send_home_launch_signal(ret);
935 #endif
936                                 g_timeout_add(1500, __add_history_handler, pkt);
937                                 free_pkt = 0;
938                         }
939
940                         if (kb != NULL)
941                                 bundle_free(kb), kb = NULL;
942
943                         break;
944 #endif
945                 case APP_RESULT:
946                 case APP_CANCEL:
947                         ret = __foward_cmd(pkt->cmd, kb, cr.pid);
948                         //__real_send(clifd, ret);
949                         close(clifd);
950                         break;
951                 case APP_PAUSE:
952                         appid = (char *)bundle_get_val(kb, AUL_K_PKG_NAME);
953                         ret = _status_app_is_running_v2_cached(appid);
954                         if (ret > 0) {
955                                 _pause_app(ret, clifd);
956                         } else {
957                                 _E("%s is not running", appid);
958                                 close(clifd);
959                         }
960                         break;
961                 case APP_RESUME_BY_PID:
962                 case APP_PAUSE_BY_PID:
963                 case APP_TERM_REQ_BY_PID:
964                         appid = (char *)bundle_get_val(kb, AUL_K_PKG_NAME);
965                         ret = __app_process_by_pid(pkt->cmd, appid, &cr, clifd);
966                         break;
967                 case APP_TERM_BY_PID_WITHOUT_RESTART:
968                 case APP_TERM_BY_PID_ASYNC:
969                         ret = security_server_check_privilege_by_sockfd(clifd, "aul::terminate", "x");
970                         if (cr.pid != 0 && ret == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) {
971                                 _E("terminate request has been denied by smack");
972                                 ret = -EILLEGALACCESS;
973                                 __real_send(clifd, ret);
974                         } else {
975                                 term_pid = (char *)bundle_get_val(kb, AUL_K_PKG_NAME);
976                                 appid = _status_app_get_appid_bypid(atoi(term_pid));
977                                 ai = appinfo_find(_raf, appid);
978                                 if(ai) {
979                                         appinfo_set_value((struct appinfo *)ai, AIT_STATUS, "norestart");
980                                         ret = __app_process_by_pid(pkt->cmd, term_pid, &cr, clifd);
981                                 } else {
982                                         ret = -1;
983                                         close(clifd);
984                                 }
985                         }
986                         break;
987                 case APP_TERM_BY_PID:
988                 case APP_KILL_BY_PID:
989                         ret = security_server_check_privilege_by_sockfd(clifd, "aul::terminate", "x");
990                         if (cr.pid != 0 && ret == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) {
991                                 _E("terminate request has been denied by smack");
992                                 ret = -EILLEGALACCESS;
993                                 __real_send(clifd, ret);
994                         } else {
995                                 appid = (char *)bundle_get_val(kb, AUL_K_PKG_NAME);
996                                 ret = __app_process_by_pid(pkt->cmd, appid, &cr, clifd);
997                         }
998                         break;
999                 case APP_TERM_BGAPP_BY_PID:
1000                         ret = security_server_check_privilege_by_sockfd(clifd, "aul::terminatebgapp", "x");
1001                         if (cr.pid != 0 && ret == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) {
1002                                 _E("terminate request has been denied by smack");
1003                                 ret = -EILLEGALACCESS;
1004                                 __real_send(clifd, ret);
1005                         } else {
1006                                 appid = (char *)bundle_get_val(kb, AUL_K_PKG_NAME);
1007                                 ret = __app_process_by_pid(pkt->cmd, appid, &cr, clifd);
1008                         }
1009                         break;
1010                 case APP_RUNNING_INFO:
1011                         _status_send_running_appinfo_v2(clifd);
1012                         break;
1013                 case APP_RUNNING_INFO_MEMORY:
1014                         _status_send_running_appinfo(clifd);
1015                         break;
1016                 case APP_IS_RUNNING:
1017                         appid = malloc(MAX_PACKAGE_STR_SIZE);
1018                         if (appid == NULL) {
1019                                 _E("Failed to allocate memory");
1020                                 __send_result_to_client(clifd, -1);
1021                                 break;
1022                         }
1023                         strncpy(appid, (const char*)pkt->data, MAX_PACKAGE_STR_SIZE-1);
1024                         ret = _status_app_is_running_v2_cached(appid);
1025                         SECURE_LOGD("APP_IS_RUNNING : %s : %d",appid, ret);
1026                         if (ret > 0 && _status_get_app_info_status(ret) == STATUS_DYING) {
1027                                 SECURE_LOGD("APP_IS_RUNNING: %d is dying", ret);
1028                                 ret = -1;
1029                         }
1030                         __send_result_to_client(clifd, ret);
1031                         free(appid);
1032                         break;
1033                 case APP_GET_APPID_BYPID:
1034                         memcpy(&pid, pkt->data, sizeof(int));
1035                         ret = _status_get_appid_bypid(clifd, pid);
1036                         _D("APP_GET_APPID_BYPID : %d : %d", pid, ret);
1037                         break;
1038                 case APP_GET_PKGID_BYPID:
1039                         memcpy(&pid, pkt->data, sizeof(int));
1040                         ret = _status_get_pkgid_bypid(clifd, pid);
1041                         _D("APP_GET_PKGID_BYPID : %d : %d", pid, ret);
1042                         break;
1043                 case APP_KEY_RESERVE:
1044                         ret = _register_key_event(cr.pid);
1045                         __send_result_to_client(clifd, ret);
1046                         break;
1047                 case APP_KEY_RELEASE:
1048                         ret = _unregister_key_event(cr.pid);
1049                         __send_result_to_client(clifd, ret);
1050                         break;
1051                 case APP_STATUS_UPDATE:
1052                         status = (int *)pkt->data;
1053                         _W("app status : %d", *status);
1054                         if(*status == STATUS_NORESTART) {
1055                                 appid = _status_app_get_appid_bypid(cr.pid);
1056                                 ai = appinfo_find(_raf, appid);
1057                                 appinfo_set_value((struct appinfo *)ai, AIT_STATUS, "norestart");
1058                         } else {
1059                                 ret = _status_update_app_info_list(cr.pid, *status);
1060                         }
1061                         //__send_result_to_client(clifd, ret);
1062                         close(clifd);
1063                         break;
1064                 case APP_GET_STATUS:
1065                         memcpy(&pid, pkt->data, sizeof(int));
1066                         ret = _status_get_app_info_status(pid);
1067                         __send_result_to_client(clifd, ret);
1068                         break;
1069
1070                 case APP_RUNNING_LIST_UPDATE:
1071                         /*appid = (char *)bundle_get_val(kb, AUL_K_APPID);
1072                           app_path = (char *)bundle_get_val(kb, AUL_K_EXEC);
1073                           tmp_pid = (char *)bundle_get_val(kb, AUL_K_PID);
1074                           pid = atoi(tmp_pid);
1075                           ret = _status_add_app_info_list(appid, app_path, pid);*/
1076                         ret = 0;
1077                         __send_result_to_client(clifd, ret);
1078                         break;
1079
1080                 case APP_GROUP_GET_WINDOW:
1081                         __dispatch_app_group_get_window(clifd, pkt);
1082                         break;
1083
1084                 case APP_GROUP_SET_WINDOW:
1085                         __dispatch_app_group_set_window(clifd, pkt, cr.pid);
1086                         break;
1087
1088                 case APP_GROUP_GET_FG:
1089                         __dispatch_app_group_get_fg_flag(clifd, pkt);
1090                         break;
1091
1092                 case APP_GROUP_GET_LEADER_PIDS:
1093                         __dispatch_app_group_get_leader_pids(clifd, pkt);
1094                         break;
1095
1096                 case APP_GROUP_GET_GROUP_PIDS:
1097                         __dispatch_app_group_get_group_pids(clifd, pkt);
1098                         break;
1099
1100                 case APP_GROUP_CLEAR_TOP:
1101                         __dispatch_app_group_clear_top(clifd, cr.pid);
1102                         break;
1103
1104                 case APP_GROUP_GET_LEADER_PID:
1105                         __dispatch_app_group_get_leader_pid(clifd, pkt);
1106                         break;
1107
1108                 case APP_GROUP_LOWER:
1109                         __dispatch_app_group_lower(clifd, cr.pid);
1110                         break;
1111
1112                 case APP_GROUP_GET_IDLE_PIDS:
1113                         __dispatch_app_group_get_idle_pids(clifd, pkt);
1114                         break;
1115
1116                 case APP_GET_CMDLINE:
1117                         memcpy(&pid, pkt->data, sizeof(int));
1118                         ret = _status_get_cmdline(clifd, pid);
1119                         _D("APP_GET_CMDLINE : %d : %d", pid, ret);
1120                         break;
1121
1122                 case APP_GET_PID:
1123                         appid = (char *)malloc(MAX_PACKAGE_STR_SIZE);
1124                         if (appid == NULL) {
1125                                 _E("failed to allocate appid");
1126                                 __send_result_to_client(clifd, -1);
1127                                 break;
1128                         }
1129                         strncpy(appid, (const char *)pkt->data, MAX_PACKAGE_STR_SIZE - 1);
1130                         ret = _status_app_is_running_v2_cached(appid);
1131                         SECURE_LOGD("APP_GET_PID: %s : %d", appid, ret);
1132                         __send_result_to_client(clifd, ret);
1133                         free(appid);
1134                         break;
1135
1136                 case APP_GET_PID_CACHE:
1137                         appid = (char *)malloc(MAX_PACKAGE_STR_SIZE);
1138                         if (appid == NULL) {
1139                                 _E("failed to allocate appid");
1140                                 __send_result_to_client(clifd, -1);
1141                                 break;
1142                         }
1143                         strncpy(appid, (const char *)pkt->data, MAX_PACKAGE_STR_SIZE - 1);
1144                         ret = _status_app_is_running_from_cache(appid);
1145                         SECURE_LOGD("APP_GET_PID_CACHE: %s : %d", appid, ret);
1146                         __send_result_to_client(clifd, ret);
1147                         free(appid);
1148                         break;
1149
1150                 case APP_GET_LAST_CALLER_PID:
1151                         memcpy(&pid, pkt->data, sizeof(int));
1152                         ret = _status_get_app_info_last_caller_pid(pid);
1153                         SECURE_LOGD("APP_GET_LAST_CALLER_PID: %d : %d", pid, ret);
1154                         __send_result_to_client(clifd, ret);
1155                         break;
1156                 case APP_SET_PROCESS_GROUP:
1157                         owner_pid = atoi(bundle_get_val(kb, AUL_K_OWNER_PID));
1158                         child_pid = atoi(bundle_get_val(kb, AUL_K_CHILD_PID));
1159                         ret = _send_set_process_group_signal_signal(owner_pid, child_pid);
1160                         if (kb != NULL)
1161                                 bundle_free(kb), kb = NULL;
1162                         __send_result_to_client(clifd, ret);
1163                         break;
1164                 case APP_GET_GROUP_INFO:
1165                         _status_send_group_info(clifd);
1166                         break;
1167                 default:
1168                         _E("no support packet");
1169                         close(clifd);
1170         }
1171
1172         if (free_pkt)
1173                 free(pkt);
1174
1175         if (kb != NULL)
1176                 bundle_free(kb), kb = NULL;
1177
1178         traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
1179         return TRUE;
1180 }
1181
1182 static gboolean __au_glib_check(GSource *src)
1183 {
1184         GSList *fd_list;
1185         GPollFD *tmp;
1186
1187         fd_list = src->poll_fds;
1188         do {
1189                 tmp = (GPollFD *) fd_list->data;
1190                 if ((tmp->revents & (POLLIN | POLLPRI)))
1191                         return TRUE;
1192                 fd_list = fd_list->next;
1193         } while (fd_list);
1194
1195         return FALSE;
1196 }
1197
1198 static gboolean __au_glib_dispatch(GSource *src, GSourceFunc callback,
1199                 gpointer data)
1200 {
1201         callback(data);
1202         return TRUE;
1203 }
1204
1205 static gboolean __au_glib_prepare(GSource *src, gint *timeout)
1206 {
1207         return FALSE;
1208 }
1209
1210 static GSourceFuncs funcs = {
1211         .prepare = __au_glib_prepare,
1212         .check = __au_glib_check,
1213         .dispatch = __au_glib_dispatch,
1214         .finalize = NULL
1215 };
1216
1217 #ifdef _APPFW_FEATURE_SEND_HOME_LAUNCH_SIGNAL
1218 static void __home_appid_vconf_cb(keynode_t *key, void *data)
1219 {
1220         char *tmpstr;
1221
1222         tmpstr = vconf_keynode_get_str(key);
1223         if (tmpstr == NULL) {
1224                 return;
1225         }
1226
1227         if (home_appid) {
1228                 free(home_appid);
1229         }
1230         home_appid = strdup(tmpstr);
1231 }
1232 #endif
1233
1234 static int __signal_get_sigchld_fd(sigset_t mask)
1235 {
1236         int sfd;
1237
1238         sfd = signalfd(-1, &mask, SFD_NONBLOCK | SFD_CLOEXEC);
1239
1240         if (sfd == -1) {
1241                 _E("failed to create signalfd for SIGCHLD");
1242                 return -1;
1243         }
1244
1245         return sfd;
1246 }
1247
1248 static void __release_app(int pid)
1249 {
1250         const char *pkg_status;
1251         const char *appid = NULL;
1252         const struct appinfo *ai = NULL;
1253
1254         appid = _status_app_get_appid_bypid(pid);
1255         ai = appinfo_find(_raf, appid);
1256         pkg_status = appinfo_get_value(ai, AIT_STATUS);
1257         SECURE_LOGI("appid: %s", appid);
1258         if (ai && pkg_status && strncmp(pkg_status, "blocking", 8) == 0) {
1259                 appinfo_set_value((struct appinfo *)ai, AIT_STATUS, "restart");
1260         } else if (ai && pkg_status && strncmp(pkg_status, "norestart", 9) == 0) {
1261                 appinfo_set_value((struct appinfo *)ai, AIT_STATUS, "installed");
1262         } else {
1263                 if (appid != NULL && __check_restart(appid)) {
1264                         _release_srv(appid);
1265                 }
1266         }
1267
1268         __send_app_termination_signal(pid);
1269 }
1270
1271 static gboolean __sigchld_handler(gpointer data)
1272 {
1273         int fd = (int)data;
1274         struct signalfd_siginfo si;
1275
1276         while (1) {
1277                 int nr = read(fd, &si, sizeof(struct signalfd_siginfo));
1278
1279                 if (nr != sizeof(struct signalfd_siginfo))
1280                         break;
1281                 while (1) {
1282                         int status;
1283                         pid_t pid = waitpid(-1, &status, WNOHANG);
1284
1285                         if (pid <= 0)
1286                                 break;
1287                         _D("Sig child %d", pid);
1288                         __release_app(pid);
1289                 }
1290
1291         }
1292
1293         return TRUE;
1294 }
1295
1296 int _signal_block_sigchld(void)
1297 {
1298         sigset_t mask;
1299
1300         sigemptyset(&mask);
1301         sigaddset(&mask, SIGCHLD);
1302
1303         if (sigprocmask(SIG_BLOCK, &mask, &oldmask) == -1) {
1304                 _E("failed to sigprocmask");
1305                 return -1;
1306         }
1307
1308         return __signal_get_sigchld_fd(mask);
1309 }
1310
1311 int _signal_unblock_sigchld(void)
1312 {
1313         if(sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
1314                 _E("SIG_SETMASK error");
1315                 return -1;
1316         }
1317
1318         _D("SIGCHLD unblocked");
1319         return 0;
1320 }
1321
1322 int _request_init(struct amdmgr *amd, int fd_sig)
1323 {
1324         int fd;
1325         int r;
1326         GPollFD *gpollfd;
1327         GPollFD *gpollfd_sig;
1328         GSource *src;
1329         GSource *src_sig;
1330         DBusError error;
1331
1332         fd = __create_server_sock(AUL_UTIL_PID);
1333         if (fd < 0) {
1334                 _E("fail to create server sock");
1335                 return -1;
1336         }
1337         src = g_source_new(&funcs, sizeof(GSource));
1338
1339         gpollfd = (GPollFD *) g_malloc(sizeof(GPollFD));
1340         if (gpollfd == NULL) {
1341                 g_source_unref(src);
1342                 close(fd);
1343                 return -1;
1344         }
1345
1346         gpollfd->events = POLLIN;
1347         gpollfd->fd = fd;
1348
1349         g_source_add_poll(src, gpollfd);
1350         g_source_set_callback(src, (GSourceFunc) __request_handler,
1351                         (gpointer) gpollfd, NULL);
1352         g_source_set_priority(src, G_PRIORITY_DEFAULT);
1353
1354         r = g_source_attach(src, NULL);
1355         if (r  == 0) {
1356                 _E("fail to attach the source : %d", r);
1357                 return -1;
1358         }
1359
1360         src_sig = g_source_new(&funcs, sizeof(GSource));
1361
1362         gpollfd_sig = (GPollFD *) g_malloc(sizeof(GPollFD));
1363         if (gpollfd_sig == NULL) {
1364                 g_source_unref(src_sig);
1365                 close(fd_sig);
1366                 return -1;
1367         }
1368
1369         gpollfd_sig->events = G_IO_IN;
1370         gpollfd_sig->fd = fd_sig;
1371
1372         g_source_add_poll(src_sig, gpollfd_sig);
1373         g_source_set_callback(src_sig, (GSourceFunc) __sigchld_handler,
1374                         (gpointer) fd_sig, NULL);
1375         g_source_set_priority(src_sig, G_PRIORITY_DEFAULT);
1376         r = g_source_attach(src_sig, NULL);
1377         if (r  == 0) {
1378                 _E("fail to attach the source : %d", r);
1379                 return -1;
1380         }
1381
1382         _raf = amd->af;
1383
1384         r = rua_init();
1385         r = rua_clear_history();
1386
1387         _D("rua_clear_history : %d", r);
1388
1389         dbus_error_init(&error);
1390         bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
1391
1392 #ifdef _APPFW_FEATURE_SEND_HOME_LAUNCH_SIGNAL
1393         home_appid = vconf_get_str(VCONFKEY_SETAPPL_SELECTED_PACKAGE_NAME);
1394         if (vconf_notify_key_changed(VCONFKEY_SETAPPL_SELECTED_PACKAGE_NAME, __home_appid_vconf_cb, NULL) != 0) {
1395                 _E("Unable to register callback for VCONFKEY_SETAPPL_SELECTED_PACKAGE_NAME\n");
1396         }
1397 #endif
1398
1399         restart_tbl = g_hash_table_new(g_str_hash, g_str_equal);
1400         return 0;
1401 }
1402
1403